Instagram
youtube
Facebook
Twitter

ExpressJS- POST Request

Express.js POST Request

  • POST is a common HTTP request used for building REST API.
  • It is used to send large amounts of data.
  • The POST method is used to send large amounts of data because data is sent in the body.
  • Example

In index.html

<html>  
<body>  
<form action="http://127.0.0.1:8000/process_post" method="POST">  
First Name: <input type="text" name="first_name">  <br>  
Last Name: <input type="text" name="last_name">  
<input type="submit" value="Submit">  
</form>  
</body>  
</html>  

In example.js

var express = require('express');  
var app = express();  
var bodyParser = require('body-parser');  
var urlencodedParser = bodyParser.urlencoded({ extended: false })  
app.use(express.static('public'));  
app.get('/index.html', function (req, res) {  
   res.sendFile( __dirname + "/" + "index.html" );  
})  
app.post('/process_post', urlencodedParser, function (req, res) {  
   response = {  
       first_name:req.body.first_name,  
       last_name:req.body.last_name  
   };  
   console.log(response);  
   res.end(JSON.stringify(response));  
})  
var server = app.listen(8000, function () {  
  var host = server.address().address  
  var port = server.address().port  
  console.log("Example app listening at http://%s:%s", host, port)  
})  

Run the server by command on terminal

node app.js

Fill in the information and click on submit