Developing a POST request in nodejs

Unlike GET request which sends the data through URL may not be considered the right option in case of sending a confidential data, POST request is considered the best option as it encrypts the data in the body to send it.

Nodejs is a one of a kind where you can easily create a POST request. Let’s learn how to create a POST request.

1. We will create a request to get the user name. Let’s name the server file as server.js

server.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 })


//The / here represents the path to an interface to send the user name from

    app.get('/', function (req, res) {

        res.sendFile( __dirname + "/" + "index.html" );

    })


app.post('/listUsers', urlencodedParser, function (req, res) {

    // Prepare output in JSON format

    response = {

        first_name:req.body.first_name,

    last_name:req.body.last_name

    };

    console.log(response);

    res.end(JSON.stringify(response));

})


app.listen('8081');

console.log("server is up and running..");

The express module enables you to create a server while the body parser allows you to parse the body in which the user name is sent. Since we need to encode the body, the urlencoded does the job for us.

The post request uses headers to send the data, you may need different types of headers depending upon your requirement(for instance, multipart-form-data to send files and x-www-form-urlencoded to send form data). For more info about headers, click here

req.body refers to the content of the body. If you’d like to fetch specific contents from the body, you may add it at the end of req.body as in the above code where we’re fetching first_name using req.body.first_name.

2. To send a request, we first create an interface where we can send the username from.

index.html

<html>

<body>

<form action = "http://localhost:8081/listUsers" method = "POST">

 First Name: <input type = "text" name = "first_name">&nbsp; <br>

 Last Name: <input type = "text" name = "last_name">

 <input type = "submit" value = "Submit">

</form>

</body>

3. Running the server

Now that we are ready with server and interface, let’s run them.

To run the node server, type the following command(presuming you have nodejs installed or you may install it from here)

nodeserver.js

Output

server is up and running..

4. To send a request using the interface, open http:localhost:8081 in your browser and you will be redirected to webpage where you can input username


4. Click on “Submit“, which sends a request to the server and you’ll get response that returns the username

If you notice the url in the output, you don’t find the user name unlike in GET request you’ll find the user name in the URL itself. The user name in POST request in embedded in the body which is why it doesn’t appear in the URL. Thus, POST request is considered to be the best practice while sending confidential data.

Here we are representing them in JSON format. Similarly, we may save it to the file or database which I’ll demonstrate in the upcoming posts.

Happy coding! 😉

Start a Conversation

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