How HTTP POST request work in node.js ?

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

const express = require("express");
const app = express();
  
// Define routes here ...
  
app.listen(3000, function(){
  console.log("server is running on port 3000");
})

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

Handle POST Route in Express.js:
Filename: app.js

filter_none

edit
close

play_arrow

link
brightness_4
code

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");
})

chevron_right


Steps To Run:

  • npm init 
  • npm install express
  • npm install body-parser
  • node app.js
  • point your browser to http://localhost:3000

Output: 




My Personal Notes arrow_drop_up


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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.