Process Data over HTTP in Node.js

Posted by: Mahesh Sabnis , on 12/2/2015, in Category Node.js
Views: 15937
Abstract: Using Node.js to process the data posted via a HTML page by the client. We will check the request method GET and POST and respond accordingly.

Node.js provides several built-in modules for various server side operations. For example, we can create web server in Node.js using http module, perform File IO operations using fs module and so on. When we create Web server using Node.js and manage requests processing through it, we can also store Html files on this server and deliver to a client’s http request. However what if the client wants to post some data via a html page and expects the Node.js web server to process it? Yes we can do this too by implementing the request method check e.g. GET, POST, etc.


In this article, we will implement Node.js application which will process the data posted by the client using the HTML page delivered to it.

This application is developed using Visual Studio Code. This is a new FREE cross-platform editor for building and debugging modern web and cloud applications. We also need Node.js tools so that we can install necessary Node modules. Please visit following links to grab related information about Visual Studio Code.

Download Node tools from the following link

Node.js Implementation

Step 1: Create a folder on your hard drive (say E:\) with the name VSCodeProcessPostedData. Under this folder, add a new folder of name Scripts. We will be using this subfolder to store our application files. Open Visual Studio Code. Open the VSCodeProcessPostedData folder using File > Open Folder Option. Once the folder is opened, the option for creating new file will be displayed as shown in the following image. Select the Script folder and click on the new file icon as shown in the following image. This will provide a blank textbox where you can create a new file and call it as appjs.


Step 2: In the VSCodeProcessPostedData folder, add a folder of name AppPages. In this folder add a new HTML file of name MyPage.html. Add the following markup in it:

<html xmlns="">
    <meta charset="utf-8" />
    <title>The Page Returned by Making Http Call to Node.js</title>
    <style type="text/css"> 
        table, td {
    <h1>Product Information Page</h1>
    <form method="post">
                <td>Product Id:</td>
                    <input type="text" name="pid" id="pid"/>
                <td>Product Name:</td>
                    <input type="text" name="pname" id="pname"/>
                    <input type="submit" value="Save" />


This is a simple HTML file which will be sent to the client upon request.

Step 3: Open app.js and add the following code in it:

var http = require('http');
var fs = require('fs');

var productPage = fs.readFileSync('./AppPages/MyPage.html');

var server = http.createServer(function (req, resp) {
    if (req.method === "GET") {
        resp.writeHead(200, { 'content-type': 'text/html' });
    if (req.method === "POST") {
        var productData = '';
        req.on('data', function (prd) {
            productData += prd;
        }).on('end', function () {
            console.log('The received data is ' + productData.toString());
            resp.end('Data received  from you is ' + productData.toString());
console.log('Server started on  5050');

The above code performs the following operations (Note: Comments on each line match with the numbers used in the following points):

1. Load the http and fs module. These modules will be used to create web server and for performing file operations.

2. Read the html file synchronously, so that after the completion of the read operation, the request processing can be started.

3. Create an http server for request processing.

4. Detect the request method, if it is GET then respond with the Html file for the request.

5. If the request is of the type POST, then read the posted data and process it. On the request end the data will be displayed on the console on the server side and will be responded back to the client’s request.



Step 4: Right-Click on app.js and select Open in Command Prompt option. This will show the command prompt from where we can run the application.


On this command prompt, enter the following command

Node app

The following result will be displayed:


Step 5: Open any browser and enter the following URL in the address bar


The following result will be displayed:


Enter Product Id as 101 and Product Name as Laptop and click on the Save button, the following result will be displayed


This shows the posted data sent back to the client. We can see the posted data on the server side as well, as shown in following image



In Node.js using http module we can not only deliver a html response to the client, but also process the data posted by the client.

Download the entire source code of this article (Github)

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!