Open In App

How HTTP POST requests work in Node ?

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The HTTP POST method is used to send data from the client to the server. Unlike GET, which appends data in the URL, POST sends data in the request body, which makes it ideal for form submissions, file uploads, and secure data transfers.

In Node.js, handling POST requests is commonly done using the Express.js framework, which simplifies routing and request handling.

HTTP Post in Node

In Node.js, the POST method is typically implemented using the Express framework. Express creates an app server and provides the app.post method to handle POST.

Syntax:

app.post(route, function(req, res){
//this is a callback function
})
  • req (request): contains the data sent by the client
  • res (response): used to send data back to the client

To make the http request we uses the we can use axios, node-fetch, and http module. Check this article to know how to make http requests in Node.

Steps to Create the Application

Step 1: Initialising the Node App using the below command:

npm init -y

Step 2: Installing the required packages:

npm i express body-parser

Project Structure:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
"express": "^5.1.0",
}

Example: Below is the basic example of the HTTP POST request using nodejs:

  • User accesses the server at http://localhost:3000/.
  • The server sends the HTML form for the user to input two numbers.
  • User enters numbers and submits the form.
  • The server receives the POST request, extracts the numbers, performs addition, and sends the result as a response.
HTML
// index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Calculator</title>
</head>
<body>
    <h1>Simple Calculator</h1>
    <form action="/" method="post">
        <input type="text" name="num1" placeholder="First Number" required>
        <input type="text" name="num2" placeholder="Second Number" required>
        <button type="submit">Calculate</button>
    </form>
</body>
</html>

</html>
JavaScript
const express = require('express');
const app = express();
const port = 3000;

// Use built-in middleware to parse URL-encoded data (form submissions)
app.use(express.urlencoded({ extended: true }));

// Route to serve the HTML form
app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

// Handle POST request
app.post('/', (req, res) => {
    const num1 = parseFloat(req.body.num1);
    const num2 = parseFloat(req.body.num2);
    const result = num1 + num2;

    res.send(`The result is: ${result}`);
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});

Output:

httppostGIF

Output

Conclusion

The https post request is created using the express app.post method in backend. This methods sets a callback for the defined route to process the request received from the client side.




Next Article

Similar Reads