Express.js POST

GET and POST both are two common HTTP requests used for building REST API's. POST requests are used to send large amount of data.

Express.js facilitates you to handle GET and POST requests using the instance of express.

Express.js POST Method

Post method facilitates you to send large amount of data because data is send in the body. Post method is secure because data is not visible in URL bar but it is not used as popularly as GET method. On the other hand GET method is more efficient and used more than POST.

Let's take an example to demonstrate POST method.


Fetch data in JSON format

File: Index.html


First Name:
Last Name:

File: post_example1.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.get('/index.html', function (req, res) {
   res.sendFile( __dirname + "/" + "index.html" );
})'/process_post', urlencodedParser, function (req, res) {
   // Prepare output in JSON format
   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)
Post Request

Open the page index.html and fill the entries:

Post Request

Now, you get the data in JSON format.

Post Request Post Request
Note: In the above picture, you can see that entries are not visible in the URL bar unlike GET method.
Related Tutorial
Follow Us #