Performance: DELETE route handling
This affects server response time and client perceived interaction speed when deleting resources via HTTP DELETE requests.
app.delete('/items/:id', async (req, res) => { const id = req.params.id; const itemIndex = items.findIndex(item => item.id === id); if (itemIndex === -1) { return res.status(404).send('Not found'); } // Simulate async DB delete await new Promise(resolve => setTimeout(resolve, 10)); items.splice(itemIndex, 1); res.status(204).send(); });
app.delete('/items/:id', (req, res) => { // Synchronous blocking operation const id = req.params.id; const itemIndex = items.findIndex(item => item.id === id); if (itemIndex === -1) { return res.status(404).send('Not found'); } // Simulate blocking DB delete for (let i = 0; i < 1e9; i++) {} // blocking loop items.splice(itemIndex, 1); res.status(204).send(); });
| Pattern | Server Blocking | Event Loop Impact | Response Time | Verdict |
|---|---|---|---|---|
| Synchronous blocking DELETE handler | Yes | Blocks event loop | High latency | [X] Bad |
| Asynchronous non-blocking DELETE handler | No | Event loop free | Low latency | [OK] Good |
DELETE route in Express?/items/:id in Express?app.delete is correct./items/:id with a callback function matches Express route syntax.DELETE /users/5 and the user with id 5 exists?
const users = [{id: 1}, {id: 5}, {id: 9}];
app.delete('/users/:id', (req, res) => {
const id = Number(req.params.id);
const index = users.findIndex(u => u.id === id);
if (index !== -1) {
users.splice(index, 1);
res.status(200).send('User deleted');
} else {
res.status(404).send('User not found');
}
});app.delete('/products/:id', (req, res) => {
const id = req.params.id;
const product = products.find(p => p.id === id);
if (!product) {
res.status(404).send('Product not found');
}
products = products.filter(p => p.id !== id);
res.send('Product deleted');
});const library = {
fiction: [{id: 1, title: 'A'}, {id: 2, title: 'B'}],
nonFiction: [{id: 3, title: 'C'}]
};
app.delete('/books/:id', (req, res) => {
const id = Number(req.params.id);
let found = false;
for (const genre in library) {
const index = library[genre].findIndex(book => book.id === id);
if (index !== -1) {
library[genre].splice(index, 1);
found = true;
break;
}
}
if (found) {
res.status(200).send('Book deleted');
} else {
res.status(404).send('Book not found');
}
});Pass the test and this pattern moves unknown