Creating Node.js First Application

In this tutorial let us look into how to create the application in Node.js. Basically, there are 3 simple steps to create this simple node.js application.

  1. Import Required Modules – We will be using require directive to load the HTTP node.js module. We will discuss more on modules in details later.
  2. Create Servers – We need to create a server which listens to client’s request. It’s similar to IIS or Apache HTTP server.
  3. Read the request and return response – The server created in the above step will read the HTTP request made by the client which can be a browser or console and returns the response.

Creating Node.js First Application

Step 1: Import the required module

In this example, we are building a simple web application which returns “Hello World” . We can use require directive to load the HTTP module and store it into a variable called http.

var http = require("http");

Step 2: Create the Server

Now we have loaded the HTTP module we need to create the server. This can be done by creating the instance and calling http.createServer() method and bind it to the port 8080 using the listen method. We need to pass the function arguments with request and response. To create the server follow the below steps.

  • Open the command prompt and type node to start the node.js
  • Copy the below code and paste in the Node REPL terminal and hit enter
var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   // Send the response body as "Hello World"
   response.end('Hello World\n');

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

Step 3: Open the browser and type the URL and you should be able to see the string “Hello World” in the browser as shown in the below screenshot.

Creating Node.js First Application
Creating Node.js First Application


Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like

npm in Node.js

Table of Contents Hide What is npm in Node.js?Installing Modules using npmGlobal vs Local Installation What is npm in Node.js? Node Package Manager (npm) as the name indicates it is…
View Post
Xor In Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are identical XOR outputs as 0 and when two bits are different then…
View Post