Below is a simple implementation of a TODO list in Node.js using in-memory storage and CRUD operations. Since there's no login system, all users will share the same TODO list.
Directory Structure:
todo-list/
│
├── package.json
├── server.js
└── todos.js
package.json:
{
"name": "todo-list",
"version": "1.0.0",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.17.1"
}
}
todos.js (In-memory storage and CRUD operations):
let todos = [];
let idCounter = 1;
function getTodos() {
return todos;
}
function getTodo(id) {
return todos.find(todo => todo.id === parseInt(id));
}
function createTodo(title) {
const newTodo = {
id: idCounter++,
title,
completed: false,
};
todos.push(newTodo);
return newTodo;
}
function updateTodo(id, updates) {
const todo = getTodo(id);
if (!todo) return null;
if (updates.title) todo.title = updates.title;
if (updates.completed !== undefined) todo.completed = updates.completed;
return todo;
}
function deleteTodo(id) {
const index = todos.findIndex(todo => todo.id === parseInt(id));
if (index === -1) return null;
return todos.splice(index, 1)[0];
}
module.exports = {
getTodos,
getTodo,
createTodo,
updateTodo,
deleteTodo,
};
server.js (Express server with CRUD routes):
const express = require('express');
const { getTodos, getTodo, createTodo, updateTodo, deleteTodo } = require('./todos');
const app = express();
const port = 3000;
app.use(express.json());
// Get all todos
app.get('/todos', (req, res) => {
res.json(getTodos());
});
// Get a single todo
app.get('/todos/:id', (req, res) => {
const todo = getTodo(req.params.id);
if (!todo) return res.status(404).json({ error: 'Todo not found' });
res.json(todo);
});
// Create a new todo
app.post('/todos', (req, res) => {
const { title } = req.body;
if (!title) return res.status(400).json({ error: 'Title is required' });
const newTodo = createTodo(title);
res.status(201).json(newTodo);
});
// Update a todo
app.put('/todos/:id', (req, res) => {
const { title, completed } = req.body;
const updatedTodo = updateTodo(req.params.id, { title, completed });
if (!updatedTodo) return res.status(404).json({ error: 'Todo not found' });
res.json(updatedTodo);
});
// Delete a todo
app.delete('/todos/:id', (req, res) => {
const deletedTodo = deleteTodo(req.params.id);
if (!deletedTodo) return res.status(404).json({ error: 'Todo not found' });
res.json(deletedTodo);
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Usage:
- Install dependencies:
npm install
- Start the server:
npm start
- Use a tool like
curl, Postman, or a frontend application to interact with the API.
Example API Endpoints:
- Get all todos:
GET /todos
- Get a single todo:
GET /todos/:id
- Create a new todo:
POST /todos with a JSON body like { "title": "Buy milk" }
- Update a todo:
PUT /todos/:id with a JSON body like { "completed": true }
- Delete a todo:
DELETE /todos/:id
Note that this implementation uses in-memory storage, so all data will be lost when the server restarts. For persistent storage, consider using a database like MongoDB or SQLite.