How to Accept Data from a HTTP POST Request in a NodeJS Application

Posted on

Introduction

This article will have a focus on how to create a simple NodeJS application. The main purpose is simple. It is to accept data from a HTTP Post request. By simulating it using a Postman application for generating a HTTP Post request, the service run after executing the NodeJS application will process it further.

Simulating the Process Accepting Data from a HTTP Post Request in a NodeJS Application

In order to be able to simulate it, the following is the step to achieve it :

  1. First of all, just access the machine. Accessing the machine is quite simple. Either directly or remotely. Further article informing article to remote a virtual server exist in this link. It is an article for remoting a virtual server running in a VirtualBox application with the title of ‘How to Remote CentOS Virtual Server running in a VirtualBox with a NAT Network using SSH’.

  2. Second, make sure to access the Command Line Interface (CLI) of the machine.

  3. Make sure that npm tool is available. In order to install the npm tool, just choose the suitable article in this link. An article for installing npm tool available in this link with the title of ‘How to install npm’. Another article also exist in this link with the title of ‘How to Install npm in Ubuntu Linux operating system’.

  4. Create a file in a certain folder with the name of ‘app.js’. The file has the following content :
    const express = require("express");
    const app = express();
    app.use(express.urlencoded({ extended: true} ));
    app.listen(3001);
    app.post("/save",(req, res) => {
            try {                                                                                                                                                                                         
                    const { identifier, fullname } = req.body;                
                    console.debug("Identifier : "+identifier);
                    console.debug("Full Name : "+fullname);
            } catch(err) {                                                                                                                                               
                    console.error(err.message);
                    res.status(500).send("Server Error !");
            }
    });
    
  5. Do not forget to install additional module which is important. The above script has a module with the name of ‘express’. Check the article with the title of ‘How to Install Express Module in NodeJS Application’ in this link for more information on how to install it.
  6. After that, execute the application as follows :
    [admin@10 db]$ node app.js
    
  7. Actually, the previous step is listening on an incoming HTTP POST request. So, in order to generate data so for further passing through the address listening on incoming HTTP POST request, execute Postman application as in the following image :

    How to Accept Data from a HTTP POST Request in a NodeJS Application
  8. Soon after sending the data is a success, there will appear an additional output on the console where the NodeJS application run as follows :
    [admin@10 db]$ node app.js
    Identifier : 0000000001
    Full Name : Blake Morrison
    Connected !
    ^C
    [admin@10 db]$
    

Leave a Reply