Express JS HTTP methods GET, POST, PUT, and DELETE
In Express.js and Node.js, HTTP methods define the type of action to be performed on resources identified by URLs. Here’s a detailed explanation of the commonly used HTTP methods: GET
, POST
, PUT
, and DELETE
.
1. GET
Purpose: Retrieves data from the server.
Usage: Used for requesting data without making any changes to the resource. Typically used for fetching data or rendering pages.
Example:
const express = require('express'); const app = express(); app.get('/users', (req, res) => { // Logic to retrieve user data res.send('List of users'); }); app.get('/users/:id', (req, res) => { // Logic to retrieve a specific user by ID const userId = req.params.id; res.send(`User details for ID: ${userId}`); }); app.listen(3000, () => { console.log('Server running on port 3000'); });
In this example:
/users
might return a list of users./users/:id
retrieves details for a specific user based on theid
parameter.
2. POST
Purpose: Submits data to the server to create or update a resource.
Usage: Used for sending data to the server, often resulting in the creation of a new resource or performing an action that changes the server’s state.
Example:
app.post('/users', (req, res) => { // Logic to create a new user with data from req.body res.send('User created'); });
In this example, the server handles the data submitted to
/users
(typically from a form or API client) and creates a new user based on that data.
3. PUT
Purpose: Updates a resource or creates a resource if it does not exist.
Usage: Used to update an existing resource or replace it with a new representation. If the resource doesn’t exist, it may create it (though in practice, creation is often handled by
POST
).Example:
app.put('/users/:id', (req, res) => { // Logic to update the user with the given ID const userId = req.params.id; res.send(`User with ID ${userId} updated`); });
In this example, the server updates the details for the user specified by the
id
parameter with the new data sent in the request body.
4. DELETE
Purpose: Deletes a resource from the server.
Usage: Used to remove a resource identified by a URL.
Example:
app.delete('/users/:id', (req, res) => { // Logic to delete the user with the given ID const userId = req.params.id; res.send(`User with ID ${userId} deleted`); });
In this example, the server handles the request to delete the user specified by the
id
parameter.
Summary
- GET: Retrieves data from the server. It is safe and idempotent (repeating the request will not change the state).
- POST: Submits data to create or update a resource. It is not idempotent (repeating the request may result in multiple resources or changes).
- PUT: Updates a resource or creates it if it does not exist. It is idempotent (repeating the request will result in the same state).
- DELETE: Deletes a resource. It is idempotent (repeating the request will not further change the state if the resource is already deleted).
In Express.js, you use these HTTP methods to handle requests by defining corresponding route handlers. Each method provides a way to interact with resources on the server in a RESTful manner.