CRUD Operations in Node.js
From the Express.js REST API cheat sheet ยท Routing & REST APIs ยท verified Jul 2026
CRUD Operations
Implement Create, Read, Update, Delete operations following REST conventions
javascript
// Basic CRUD routes
app.get('/api/users', (req, res) => {
res.json(users);
});
app.get('/api/users/:id', (req, res) => {
const user = users.find(u => u.id === req.params.id);
if (!user) return res.status(404).json({ error: 'User not found' });
res.json(user);
});
app.post('/api/users', (req, res) => {
const user = { id: Date.now(), ...req.body };
users.push(user);
res.status(201).json(user);
});๐ข Essential - Standard REST API operations
๐ก Use proper HTTP methods: GET (read), POST (create), PUT/PATCH (update), DELETE
๐ Return appropriate status codes: 200 OK, 201 Created, 404 Not Found
โก Use async/await for cleaner asynchronous code
โ ๏ธ Always validate and sanitize user input
๐ Related: express-validator for input validation