HTTP methods are protocols used to exchange data between different layers of an application through a request-response cycle. In web development, they allow client-side code to communicate with servers and databases, enabling data retrieval, submission, and updates without storing large amounts of data on the client side.
Types of HTTP Requests
Here are the different types of http requests
GET Method
The GET method is an HTTP request used by a client to retrieve data from the server. It takes two parameters: the URL to listen on and a callback function with req (client request) and res (server response) as arguments.
Syntax:
app.get("URL",(req,res)=>{})
In the above syntax
- app.get – defines a GET route in Express.
- "URL" – the path the route listens to.
- (req, res) => {} – callback function where req is the client request and res is the server response.
POST Method
The POST method sends data from the client to the server, usually to store it in a database. It takes two parameters: the URL to listen on and a callback function with req (client request) and res (server response). The data sent is available in the request body and must be parsed as JSON.
Syntax:
app.post("URL",(req,res)=>{})
In the above syntax
- app.post – defines a POST route in Express.
- "URL" – the path the route listens to.
- (req, res) => {} – callback function where req contains client data and res sends the server response.
PUT Method
The PUT method updates existing data in the database. It takes two parameters: the URL to listen on and a callback function with req (client request containing updated data in the body) and res (server response).
Syntax:
app.put("URL",(req,res)=>{})
In the above syntax:
- app.put – defines a PUT route in Express.
- "URL" – the path the route listens to.
- (req, res) => {} – callback function where req contains the client’s updated data (usually in the body) and res sends the server response.
DELETE Method
The DELETE method removes data from the database. It takes two parameters: the URL to listen on and a callback function with req (containing the ID of the item to delete in the body) and res (server response).
Syntax:
app.delete("URL",(req,res)=>{})
In the above syntax
- app.delete – defines a DELETE route in Express.
- "URL" – the path the route listens to.
- (req, res) => {} – callback function where req contains data (e.g., ID to delete) and res sends the server response.
Example: Implementation of above HTTP methods.
JavaScript
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.json());
app.get("/", (req, res) => {
console.log("GET Request Successfull!");
res.send("Get Req Successfully initiated");
})
app.put("/put", (req, res) => {
console.log("PUT REQUEST SUCCESSFUL");
console.log(req.body);
res.send(`Data Update Request Recieved`);
})
app.post("/post", (req, res) => {
console.log("POST REQUEST SUCCESSFUL");
console.log(req.body);
res.send(`Data POSt Request Recieved`);
})
app.delete("/delete", (req, res) => {
console.log("DELETE REQUEST SUCCESSFUL");
console.log(req.body);
res.send(`Data DELETE Request Recieved`);
})
app.listen(PORT, () => {
console.log(`Server established at ${PORT}`);
})
Output:
Explore
Node.js Tutorial
4 min read
Introduction & Installation
Node.js Modules , Buffer & Streams
Node.js Asynchronous Programming
Node.js NPM
Node.js Deployments & Communication
Resources & Tools