Performance: HTTP methods and CRUD mapping
This concept affects how efficiently the server handles requests and how quickly the client receives responses, impacting interaction speed and server load.
app.post('/items', createItem); app.get('/items/:id', getItem); app.put('/items/:id', updateItem); app.delete('/items/:id', deleteItem);
app.post('/items', (req, res) => { /* handles create, read, update, delete all here */ });
| Pattern | Server Processing | Network Efficiency | Caching | Verdict |
|---|---|---|---|---|
| Using POST for all CRUD | High (complex routing) | Low (no caching) | Poor | [X] Bad |
| Using correct HTTP methods | Low (simple routing) | High (cacheable GET) | Good | [OK] Good |
GET is used to read or retrieve data from the server.POST creates, PUT updates, and DELETE removes data, so only GET fits retrieving data.PUT method in RESTful APIs.app.put() to handle PUT requests, so app.put('/item/:id', handler) is correct.app.delete('/user/:id', (req, res) => {
res.send(`Deleted user ${req.params.id}`);
});
// Client sends DELETE request to /user/42
What will the server respond with?DELETE requests on /user/:id. The client sends a DELETE request to /user/42.Deleted user 42 using the id from the URL.app.get('/items', (req, res) => {
// code to add new item
res.send('Item created');
});GET, which is meant for reading data, not creating.POST, so using GET here is incorrect.POST /products for create, GET /products/:id for read, PUT /products/:id for update, and DELETE /products/:id for delete, which is correct.Pass the test and this pattern moves unknown