How to retrieve the POST data using Express in Node JS

Learning Node JS could lead to you too many questions. One of them is how to retrieve the POST data using Express in Node JS.

This is question arises while you send data from client application to your server. Let’s consider you have a login form and on click of submit you are making a POST request to the server with username and password in JSON format.

Lets say your JSON formatted data look like this while requesting.

{
    "username":"abc@gmail.com",
    "password":"abcdf"
}

Now, this leads back to our main question, How do I access this data in my server.

So to access above JSON data in our server, First of all, we are sending it in JSON format so we have to use the following line in our code. which is nothing but serves as middleware on each request.

app.use(express.json());

if you sending data with content-type – application/x-www-form-urlencoded then use the following line

express.urlencoded()

Now Lets say I am making request to the following URL or Route.

http://localhost:8000/store

So in my server side , Our whole would look like this.

const express = require('express');
const app = express();
const port = 8000;
app.use(express.json());

app.post('/store', (req, res) => {
	res.send(req.body);
	return;
});
app.listen(port, () => console.log(`app started on http://localhost:${port}`));

Lets me show you the output of this code . I am going to use POSTMAN to make request to server by above URL.

Learning Node JS could lead to you too many questions. One of them is how to retrieve the POST data using Express in Node JS.

This is question arises while you send data from client application to your server. Let’s consider you have a login form and on click of submit you are making a POST request to the server with username and password in JSON format.

Lets say your JSON formatted data look like this while requesting.

{
    "username":"abc@gmail.com",
    "password":"abcdf"
}

Now, this leads back to our main question, How do I access this data in my server.

So to access above JSON data in our server, First of all, we are sending it in JSON format so we have to use the following line in our code. which is nothing but serves as middleware on each request.

app.use(express.json());

if you sending data with content-type – application/x-www-form-urlencoded then use the following line

express.urlencoded()

Now Lets say I am making request to the following URL or Route.

http://localhost:8000/store

So in my server side , Our whole would look like this.

const express = require('express');
const app = express();
const port = 8000;
app.use(express.json());

app.post('/store', (req, res) => {
	res.send(req.body);
	return;
});
app.listen(port, () => console.log(`app started on http://localhost:${port}`));

Lets me show you the output of this code . I am going to use POSTMAN to make request to server by above URL.

So thats all about how to retrieve the POST data using Express in Node JS. I hope this article helped you , If you any queries regarding this article , Leave the comment in the comments section. We will surely help you out.

Thank you.