Certainly! Here’s a simple Node.js ToDo list app using only in-memory storage (no database, no login, just CRUD). This example uses Express for the server and body-parser for parsing JSON requests.
1. Setup
First, create a new directory and initialize a Node.js project:
mkdir simple-todo
cd simple-todo
npm init -y
npm install express body-parser
2. Create index.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
// In-memory storage
let todos = [];
let idCounter = 1;
app.use(bodyParser.json());
// Get all todos
app.get('/todos', (req, res) => {
res.json(todos);
});
// Get a single todo
app.get('/todos/:id', (req, res) => {
const todo = todos.find(t => t.id === parseInt(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 = { id: idCounter++, title, completed: false };
todos.push(newTodo);
res.status(201).json(newTodo);
});
// Update a todo
app.put('/todos/:id', (req, res) => {
const todo = todos.find(t => t.id === parseInt(req.params.id));
if (!todo) return res.status(404).json({ error: 'Todo not found' });
const { title, completed } = req.body;
if (title !== undefined) todo.title = title;
if (completed !== undefined) todo.completed = completed;
res.json(todo);
});
// Delete a todo
app.delete('/todos/:id', (req, res) => {
const index = todos.findIndex(t => t.id === parseInt(req.params.id));
if (index === -1) return res.status(404).json({ error: 'Todo not found' });
todos.splice(index, 1);
res.status(204).send();
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
3. Run the App
node index.js
4. Test with curl or Postman
- Create:
POST /todos with JSON body: { "title": "Buy milk" }
- Read all:
GET /todos
- Read one:
GET /todos/1
- Update:
PUT /todos/1 with JSON body: { "completed": true }
- Delete:
DELETE /todos/1
Note:
This is a simple, non-persistent, in-memory app. All data will be lost when the server restarts. If you want persistence, you’ll need to use a database or write to a file.
Let me know if you want a simple HTML frontend or more features!