• Node.js Video Tutorials

Node.js - First Application



When learning a new language or a framework, the first application to write is a Hello World program. Such a program displays the Hello World message. This illustrates the basic syntax of the language and also serves to test whether the installation of the language compiler has been correctly done or not. In this chapter, we shall write a Hello World application in Node.js.

Console Application

Node.js has a command-line interface. Node.js runtime also allows you to execute JavaScript code outside the browser. Hence, any JavaScript code can be run in a command terminal with Node.js executable.

Save the following single line JavaScript as hello.js file.

console.log("Hello World");

Open a powershell (or command prompt) terminal in the folder in which hello.js file is present, and enter the following command −

PS D:\nodejs> node hello.js
Hello World

The Hello World message is displayed in the terminal.

Creating Node.js Application

To create a "Hello, World!" web application using Node.js, you need the following three important components −

  • Import required modules − We use the require directive to load Node.js modules.

  • Create server − A server which will listen to client's requests similar to Apache HTTP Server.

  • Read request and return response − The server created in an earlier step will read the HTTP request made by the client which can be a browser or a console and return the response.

Step 1 - Import Required Module

We use the require directive to load the http module and store the returned HTTP instance into an http variable as follows −

var http = require("http");

Step 2 - Create Server

We use the created http instance and call http.createServer() method to create a server instance and then we bind it at port 3000 using the listen method associated with the server instance. Pass it a function with parameters request and response.

The createserver() method has the following syntax −

http.createServer(requestListener);

The requestlistener parameter is a function that executes whenever the server receives a request from the client. This function processes the incoming request and forms a server reponse.

The requestlistener function takes request HTTP request and response objects from Node.js runtime, and returns a ServerResponse object.

listener = function (request, response) {
   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/html'});
  
   // Send the response body as "Hello World"
   response.end('<h2 style="text-align: center;">Hello World</h2>');
};

The above function adds the status code and content-type headers to the ServerResponse, and Hello World message.

This function is used as a parameter to createserver() method. The server is made to listen for the incoming request at a particular port (let us assign 3000 as the port).

Step 3 - Testing Request & Response

Write the sample implementation to always return "Hello World". Save the following script as hello.js.

http = require('node:http');
listener = function (request, response) {
   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/html
   response.writeHead(200, {'Content-Type': 'text/html'});
  
   // Send the response body as "Hello World"
   response.end('<h2 style="text-align: center;">Hello World</h2>');
};

server = http.createServer(listener);
server.listen(3000);

// Console will print the message

console.log('Server running at http://127.0.0.1:3000/');

In the PowerShell terminal, enter the following command.

PS D:\nodejs> node hello.js

Server running at http://127.0.0.1:3000/

The program starts the Node.js server on the localhost, and goes in the listen mode at port 3000. Now open a browser, and enter http://127.0.0.1:3000/ as the URL. The browser displays the Hello World message as desired.

Localhost Hello World
Advertisements