POST is a request method supported by HTTP used by the World Wide Web. The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header. To work with HTTP verbs get, post u have to use express in the node.js.
npm i express
Note: The npm in the above commands stands for node package manager, a place from where we install all the dependencies.
So in order to use Express to address POST requests on our server, we use the app.post method and then we specify the route and we have a callback.
app.post(route, function(req, res){
})
Note: If you are going to make GET, POST request frequently in node.js, then use Postman , Simplify each step of building an API.
In this syntax, the route is where you have to post your data that is fetched from the HTML. For fetching data you can use bodyparser package.
Web Server: Create app.js in the root folder. Create your server as shown in the below example.
const express = require("express"); const app = express(); // Define routes here ... app.listen(3000, function(){ console.log("server is running on port 3000"); }) |
Handle Post Request: Here you will learn how to handle HTTP POST request and get data from the submitted form.
Create index.html in the root folder of your application and write following HTML code in it.
Filename: index.html
<!DOCTYPE html> <html lang="en" dir="ltr"> <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"> <input type="text" name="num2" placeholder="Second Number"> <button type="submit" name="submit"> calculator </button> </form> </body> </html> |
Output:

Handle POST Route in Express.js:
Filename: app.js
const express = require("express"); const bodyParser = require("body-parser") // New app using express module const app = express(); app.use(bodyParser.urlencoded({ extended:true})); app.get("/", function(req, res) { res.sendFile(__dirname + "/index.html"); }); app.post("/", function(req, res) { var num1 = Number(req.body.num1); var num2 = Number(req.body.num2); var result = num1 + num2 ; res.send("Addition - " + result); }); app.listen(3000, function(){ console.log("server is running on port 3000"); }) |
Steps To Run:
- npm init
- npm install express
- npm install body-parser
- node app.js
- point your browser to http://localhost:3000

Output:

Recommended Posts:
- HTTP Request vs HapiJS Request in Node.js
- POST request using AJAX by making Custom HTTP library
- Simple GET and POST request using Fetch API method by making custom HTTP library
- Simple POST request using the fetch API
- Difference between GET and POST request in Vanilla JavaScript
- HTTP headers | Access-Control-Request-Headers
- HTTP headers | Access-Control-Request-Method
- DELETE request using XMLHttpRequest by making Custom HTTP library
- Get request using AJAX by making Custom HTTP library
- How to make PUT request using XMLHttpRequest by making Custom HTTP library ?
- How to make simple PUT request using fetch API by making custom HTTP library ?
- Simple DELETE request using fetch API by making custom HTTP library
- Nodejs | Automatic restart NodeJs server with nodemon
- HTTP GET and POST Methods in PHP
- How to select and upload multiple files with HTML and PHP, using HTTP POST?
- How to send a GET request from PHP?
- Node.js | Request Module
- How to make JavaScript wait for a API request to return?
- Node.js | https.request() Function
- Express.js | app.get() Request Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

