Use Node.js To Create Http Web Server And Process Web Page Example

Node.js provide a lot of useful modules and functions, they are very easy to use if you master how to use them. This article will show you two examples, the first example introduce how to use node.js built-in http module to create a http web server. The second example will tell you how to install and use node.js request function to get a web page info from internet.

1. Node.js Http Web Server Example.

Because http module is a build-in module, so it has been installed automatically, we can just import and use it in JavaScript code directly.

1.1 Http Web Server Example Steps.

  1. Call require(“http”) method to import http related JavaScript library into your js code. And use a variable http_module to reference http library.
  2. Create a javascript function which will process client request and send response data back to client when http web server receive request.
  3. Call http_module.createServer method to create a http web server. Pass the javascript function created in step2 as input parameter.
  4. Call web_server.listen method to make the web server listen on a special port.
  5. Print a message in console to tell user http web server has been started.

web_server.js

// Include http module. Http module contains a lot of http related javascript functions.
var http_module = require("http");

// Create a function to process client request and send response.
function process_client_request(request, response)
{
    // Below text will be sent back to client. 
   var body_content = 'Hello this web server is implemented by Node.js';

   // This is the body content length.
   var body_length = body_content.length;

   // Send header data to client.
   response.writeHead(200, { 'Content-Type':'text/plain', 'Content-Length': body_length});
   
   // Send body text to client.
   response.end(body_content);
}

// Create web server. The web server will use above function to process client request.
var web_server = http_module.createServer(process_client_request);

// Web server listen on 8000 port.
web_server.listen(8000);

// Print server running message.
console.log("Web server is running on port 8000, press Ctrl + C to exit. ");

Save above code in a js file web_server.js, run below command in shell terminal.

C:\WorkSpace\node-js-files>node web_server.js
Web server is running on port 8000, press Ctrl + C to exit.

node js http web server started in console

Open browser, input http://localhost:8000 in address input box. Click enter then you can see below page.
access node js http web server in web browser result

READ :   How To Use Node Package Manager

2. Use Node.js Request Function To Get We Page Content.

Node.js request module is not built-in module by default, so we need to install it first.

2.1 Install Node.js Request Module.

Execute npm install request command in dos window. Then wait for npm to download and install the module.

install node js request module

When you see the text + [email protected] ( module version ) at the end of the installation process, it means the request module has been installed successfully.

2.2 Write And Execute Example Code.

Use js editor to save below code in a js file called crawl_web_page.js.

// Declare a variable that refrence node.js request module.
var request_function = require("request");

// This is the web page url that we want to retrieve.
var webpage_url = "http://www.bing.com";

// This function is used to process target url web page return result.
function process_webpage(error, response, body)
{
    // If no error occurred.
   if(!error)
   {
      /* response is an instance of class http.IncomingMessage. 
           
           https://nodejs.org/api/http.html#http_class_http_incomingmessage
      */

      // Print response status code.
      console.log('Server status code : ' + response.statusCode);

      // Print http version.
      console.log('Http version : ' + response.httpVersion);

      // Print request method.
      console.log('Request method : ' + response.method);

      // Get all header information in an array.
      var raw_headers = response.rawHeaders;

      // Get the header array length.
      var length = raw_headers.length;

      // Loop the array to get each header.
      for(var i=0;i<length;i++)
      {
         // Get header.
         var raw_header = raw_headers[i];
          
            // Print the header.
          console.log(raw_header);   
      }
      
      // Because body is too long, so we just print first 100 characters.
      body = body.substr(0, 100) + "...";
      console.log(body);
    }
}

// Use the declared variable to request web page url, and use the process_webpage function to process returned web page data.
request_function(webpage_url, process_webpage);

Open dos window, input node crawl_web_page.js, then you can get below output in dos window.

C:\WorkSpace\node-js-files>node crawl_web_page.js
Server status code : 200
Http version : 1.1
Request method : null
Cache-Control
private, max-age=0
Content-Length
117841
Content-Type
text/html; charset=utf-8
Vary
Accept-Encoding
P3P
CP="NON UNI COM NAV STA LOC CURa DEVa PSAa PSDa OUR IND"
Set-Cookie
SRCHD=AF=NOFORM; domain=.bing.com; expires=Thu, 26-Mar-2020 12:39:10 GMT; path=/
Set-Cookie
SRCHUID=V=2&GUID=E1A57CC8488A466E92F7D28A23455FA8&dmnchg=1; domain=.bing.com; expires=Thu, 26-Mar-2020 12:39:10 GMT; path=/
Set-Cookie
SRCHUSR=DOB=20180326; domain=.bing.com; expires=Thu, 26-Mar-2020 12:39:10 GMT; path=/
Set-Cookie
_SS=SID=1B7B853D6FAC622C0AA18E836E1B6342; domain=.bing.com; path=/
X-MSEdge-Ref
Ref A: 3FDE964E27F644E780372156B8DCCD54 Ref B: BJ1EDGE0108 Ref C: 2018-03-26T12:39:10Z
Set-Cookie
_EDGE_S=F=1&SID=1B7B853D6FAC622C0AA18E836E1B6342; path=/; httponly; domain=bing.com
Set-Cookie
_EDGE_V=1; path=/; httponly; expires=Sat, 20-Apr-2019 12:39:10 GMT; domain=bing.com
Set-Cookie
MUID=2BD6D06FA530653F08F2DBD1A48764BD; path=/; expires=Sat, 20-Apr-2019 12:39:10 GMT; domain=bing.com
Set-Cookie
MUIDB=2BD6D06FA530653F08F2DBD1A48764BD; path=/; httponly; expires=Sat, 20-Apr-2019 12:39:10 GMT
Date
Mon, 26 Mar 2018 12:39:09 GMT
Connection
close
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm...
(Visited 168 times, 2 visits today)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.