read post data in nodejs

How to Read POST Data in NodeJS

Many times you may need to receive and process POST data in NodeJS application. In this article, we will look at how to read POST data in NodeJS.

How to Read POST Data in NodeJS

Here are the steps to read POST data in NodeJS. We will use ExpressJS for this purpose. It is a popular NodeJS plugin that makes it easy to build web applications and websites using NodeJS.

1. Create NodeJS Server

Create app.js file to be run as NodeJS server.

$ sudo vi app.js

Add the following lines to it.

var express = require('express');
var app = express();

The above code will import ExpressJS.

Next add the following lines to parse request body properly, dependin on ExpressJS version on your system.

ExpressJS >=4.16


ExpressJS < 4.16

const bodyParser = require("body-parser");

Next, add the following line to run server on port 80.


2. Create HTML form

Next, we add an HTML form at URL /form in our server.

app.get('/form', function(request, response) {
  console.log('GET /form')
  var html = '
            <form method="post" action="/test"> 
                <input type="text" name="username" />
                <input type="text" name="email" />
                <input type="submit" value="Submit" />
  response.writeHead(200, {'Content-Type': 'text/html'})

The above code will render the following HTML form at /form URL. It will have two input textboxes and one submit button.

<form method="post" action="/test">
    <input type="text" name="username">
    <input type="text" name="email">
    <input type="submit" value="Submit">

3. Read POST data in NodeJS

Next, we will create a POST request handler in NodeJS server app.js file.'/test', function(req, res){

In the above request handler, we have accessed POST data variables using req.body object followed by name of the form element.

For example, since we use req object in our request handler, and the form fields have name attributes as username and email, we access them as req.body.username and respectively.

4. Start Server

Run NodeJS server as following command.

$ sudo node app.js

Open browser and visit http://localhost/form to view the form. Fill in a test username and email and click Submit button.

On your server console, you will see the form’s POST data values displayed.

As you can see, it is very easy to capture POST data in NodeJS. Couple of things to keep in mind. Make sure that you use the form field’s name attribute to access that element’s data in request handler. For example, a form field with name=username should be referred to as req.body.username, where req is your request object.

Also, ensure that your request handler is written within function meant for POST requests. app.get is used to process GET requests only.

Also read :

How to Uninstall NodeJS & NPM in Ubuntu
How to Setup Catch-All Subdomains in NGINX
How to Fix “Unable to Find Socket Transport SSL” Error in PHP
How to Enable PHP Error Reporting
How to Redirect URL/Page with Anchor Link

Leave a Reply

Your email address will not be published. Required fields are marked *