HTTP methods tell a server what action to perform. CRUD mapping helps connect these actions to common database tasks.
HTTP methods and CRUD mapping in Node.js
GET /resource // Read data POST /resource // Create new data PUT /resource/:id // Update existing data fully PATCH /resource/:id // Update existing data partially DELETE /resource/:id // Delete data
Use GET to get or read data without changing it.
POST adds new data, PUT replaces data, and PATCH changes part of data.
GET /users
POST /users
PUT /users/123DELETE /users/123This Node.js Express app shows how HTTP methods map to CRUD actions on a simple user list.
GET reads users, POST adds a user, PUT updates a user fully, and DELETE removes a user.
import express from 'express'; const app = express(); app.use(express.json()); let users = [{ id: 1, name: 'Alice' }]; // Read all users app.get('/users', (req, res) => { res.json(users); }); // Create a new user app.post('/users', (req, res) => { const newUser = { id: Date.now(), name: req.body.name }; users.push(newUser); res.status(201).json(newUser); }); // Update a user fully app.put('/users/:id', (req, res) => { const id = Number(req.params.id); const index = users.findIndex(u => u.id === id); if (index === -1) return res.status(404).send('User not found'); users[index] = { id, name: req.body.name }; res.json(users[index]); }); // Delete a user app.delete('/users/:id', (req, res) => { const id = Number(req.params.id); users = users.filter(u => u.id !== id); res.status(204).send(); }); app.listen(3000, () => console.log('Server running on port 3000'));
Always use express.json() middleware to parse JSON request bodies.
Use status codes like 201 for created and 204 for no content to communicate results clearly.
PUT replaces the whole resource; PATCH (not shown) updates parts of it.
HTTP methods map to CRUD: GET=Read, POST=Create, PUT=Update, DELETE=Delete.
Use these methods to build clear and standard web APIs.
Express makes it easy to connect HTTP methods to code actions.
Practice
Solution
Step 1: Understand HTTP methods and their purposes
In RESTful APIs,GETis used to read or retrieve data from the server.Step 2: Match the method to the action
POSTcreates,PUTupdates, andDELETEremoves data, so onlyGETfits retrieving data.Final Answer:
GET -> Option AQuick Check:
Read = GET [OK]
- Confusing POST with GET
- Using DELETE to fetch data
- Thinking PUT retrieves data
Solution
Step 1: Identify the HTTP method for updating
Updating a resource uses thePUTmethod in RESTful APIs.Step 2: Match Express method to HTTP method
Express usesapp.put()to handlePUTrequests, soapp.put('/item/:id', handler)is correct.Final Answer:
app.put('/item/:id', handler) -> Option CQuick Check:
Update = PUT = app.put() [OK]
- Using app.post() for updates
- Using app.get() for updates
- Confusing app.delete() with update
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?Solution
Step 1: Understand the route and HTTP method
The route listens forDELETErequests on/user/:id. The client sends aDELETErequest to/user/42.Step 2: Analyze the response sent
The handler sends back the stringDeleted user 42using theidfrom the URL.Final Answer:
"Deleted user 42" -> Option BQuick Check:
DELETE request responds with deletion message [OK]
- Confusing DELETE with POST response
- Expecting 404 instead of success message
- Mixing up URL parameters
app.get('/items', (req, res) => {
// code to add new item
res.send('Item created');
});Solution
Step 1: Identify the HTTP method used
The route usesGET, which is meant for reading data, not creating.Step 2: Match method to CRUD action
Creating new data should usePOST, so usingGEThere is incorrect.Final Answer:
GET method should not be used to create data -> Option DQuick Check:
Create = POST, not GET [OK]
- Using GET to create resources
- Ignoring HTTP method semantics
- Assuming route path is the issue
Solution
Step 1: Match each CRUD action to correct HTTP method
Create = POST, Read = GET, Update = PUT, Delete = DELETE.Step 2: Check route paths and handlers
app.post('/products', createHandler); app.get('/products/:id', readHandler); app.put('/products/:id', updateHandler); app.delete('/products/:id', deleteHandler); usesPOST /productsfor create,GET /products/:idfor read,PUT /products/:idfor update, andDELETE /products/:idfor delete, which is correct.Final Answer:
app.post('/products', createHandler); app.get('/products/:id', readHandler); app.put('/products/:id', updateHandler); app.delete('/products/:id', deleteHandler); -> Option AQuick Check:
POST=create, GET=read, PUT=update, DELETE=delete [OK]
- Mixing POST and GET methods
- Using wrong paths for actions
- Assigning update to POST instead of PUT
Ready to prove it?
Pass the test and this pattern moves unknown
