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
app.use(express.bodyParser());
ExpressJS < 4.16
const bodyParser = require("body-parser");
app.use(bodyParser);
Next, add the following line to run server on port 80.
app.listen(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 = ' <html> <body> <form method="post" action="/test"> <input type="text" name="username" /> <input type="text" name="email" /> <input type="submit" value="Submit" /> </form> </body> </html>' response.writeHead(200, {'Content-Type': 'text/html'}) response.end(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"> </form>
3. Read POST data in NodeJS
Next, we will create a POST request handler in NodeJS server app.js file.
app.post('/test', function(req, res){console.log(req.body.username);
console.log(req.body.email);
});
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 req.body.email 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 app.post 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
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.