How to NodeJS- Run First Node.Js Web Server

In this tutorial, we are going to learn how to run a Node.js web server using command prompt and using Webstorm IDE.

There can be console-based and web-based node.js applications. Console based node.js example is about as plain as it can get. It prints the words “Hello World” to the terminal.

Console based node.js example is about as plain as it can get. It prints the words “Hello World” to the terminal.

Hello World Example
    // Call the console.log function.
    console.log("Hello World");

In above example, console.log() function displays “Hello World” message on console.

A node.js web application contains the following three parts:

  1. Import required modules: The “require” directive is used to load a Node.js module.
  2. Create server: You have to establish a server which will listen to client’s request similar to Apache HTTP Server.
  3. Read request and return the response: Server created in the second step will read HTTP request made by the client which can be a browser or console and return the response.

Run Node.js Web Server using Command prompt

Node.js provides an HTTP module which can be used to create an HTTP client of a server.

Create a js static file named server.js

server.js
    // All these libraries are required for the program.
    var http = require('http');
    var fs = require('fs');
    var url = require('url');

    // Create a server
    http.createServer( function (request, response) {  
       // Parse the request containing file name
       var pathname = url.parse(request.url).pathname;
   
       // Print the name of the file for which request is made.
       console.log("Request for " + pathname + " received.");
   
       // Read the requested file content from file system
       fs.readFile(pathname.substr(1), function (err, data) {
          if (err) {
             console.log(err);
             //if the file does not exist we send a “404 Page Not Found” error.
             response.writeHead(404, {'Content-Type': 'text/html'});
          }else {	
             //If the page is found then we read the file by the readFile
             response.writeHead(200, {'Content-Type': 'text/html'});	
         
             // Write the content of the file to response body
             response.write(data.toString());		
          }
          // Send the response body 
          response.end();
       });   
    }).listen(8081);

    // Console will print the message
    console.log('Server running at localhost:8081');

 

create the following Html file named index.htm in the same directory where you created server.js.

index.html
   <html>
     <head>
        <title>Lauyou Learning</title>
     </head>   
     <body>
        <h1>Lauyou Learning</h1>
        <p>Learn Yourself!</p>
     </body>
   </html>

 

Now open up the command prompt and run node server.js . Now, console saying ‘server running at localhost:8081′ which means the server is running and listening HTTP request through port 8081. Now open up your browser, type http://localhost:8081/index.html to view the Html page running on node.js server.

 

Node.Js Web Server

 

Run Node.js Web Server using Webstorm IDE

In the previous tutorial, we’ve installed web storm IDE for our node.js development. which load everything we need to use node.js web application. let’s start node.js server, open up our Webstorm IDE. We have a project named first-project which we created in the previous tutorial.

To start the server using Webstorm IDE, click Run ‘bin/www’ from Webstorm menu. You will get a message on Webstorm console window that saying ‘server Listening on port 3000′ which means the server is running and listening HTTP request through port 3000.

 

Node.Js Web Server

 

Now open up your browser, type http://localhost:3000 to view the Html page running on node.js server. Node.js

Node.Js Web Server

 

You will see the following message on Webstorm console when you open the static page.

Console Message
    first-project:server Listening on port 3000 +0ms
    GET / 200 5972.058 ms - 170
    GET /stylesheets/style.css 200 21.289 ms - 111
    GET /favicon.ico 404 70.881 ms - 1235

 

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...