PUT and DELETE API in Express.js — a practical guide to Express PUT DELETE API with clear examples you can reuse in real projects.
PUT and DELETE API in Express.js — 39/55 in the Node.js series. Full playlist companion: Node.js complete course on YouTube. Prefer one article? Read the complete Node.js tutorial.
Short description
PUT/PATCH updates an existing resource. DELETE removes it. Use route params like `/api/notes/:id`.
PUT + DELETE
app.put('/api/notes/:id', (req, res) => {
const id = Number(req.params.id);
const note = notes.find((n) => n.id === id);
if (!note) return res.status(404).json({ error: 'Not found' });
note.text = req.body.text;
res.json(note);
});
app.delete('/api/notes/:id', (req, res) => {
const id = Number(req.params.id);
const index = notes.findIndex((n) => n.id === id);
if (index === -1) return res.status(404).json({ error: 'Not found' });
notes.splice(index, 1);
res.status(204).end();
});