After describing what Node.js is, why and where to use it, followed by the three different installation processes of Node, now we are all set to code our first Node program. Node program is nothing but a JavaScript program, just in a Node way. Lets see how to do that.

Scaffolding a Node app

Node project requires a package.json file make the basic setups. We will make this file using nmp (node packaged modules), which we installed during our installation.

Follow the steps below to scxaffold your app

  1. Create a folder, inside which you will make your app.
  2. Open your terminal (or command prompt).
  3. Navigate to the folder in your terminal.
  4. Now type the following command:
    npm init

    After hitting enter or return it will ask you name, version, description, entry point, test command, git repository, author etc. You can use default values for each of them; but atleast put your name in the author column.After inserting all those it will show you the created json file. You just hit enter or return to accept that.

  5. You can also include other modules in your project. We’ve have described that in the lower part of this article using express as an external module.

On the way to writting the code

Now we are actually going to create the physical file where we are going to type our code.

Step 1:

Now go inside of your project folder and create index.js (if you changed the entry point while scaffolding, create a file with that name, instead of index.js)

Step 2:

Open the index.js file in any text editor. Now you are ready to write the code.

How to code in Node

Step 1: Including a module

You include a module ‘http’ in your code. ‘http’ is a predefined module in Node.

var http = require('http');

Step 2: Setting up using createServer

createServer is a function in http module which we have included in the first step. createServer uses a callback function with first parameter as request and second as response.
You can write the response head and also can add some text.

var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");

Step 3: Listen to a server port

The ‘server’ object, which is nothing but the object returned by http.createServer() function has a method named ‘listen’ to make the server listen to a particular port.


Here is how the code looks like

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");

// Listen on port 8000, IP defaults to

Run your Node code

Follow the steps below to run the node code.

Step 1:

Open your terminal (or command prompt) and navigate to the parent folder of your project folder.

Step 2:

execute the following command

node YourProjectFolderName

Step 3:

Your Node server is running now. Open your browser and go to http://localhost:80000/
You will find “Hello World” written there.

Another way:

You can navigate inside your project folder via your terminal and execute “node YourFileNameWithoutExtersion”. This will also do the same.

Step 4:

To stop the server, you can type command+c or ctrl+c in the terminal (or command prompt).

Install your first module

Node built-in libraries are designed to be very low level; this approach makes Node light and fast, but at the same time hard to use. That’s why you often finds yourself using an external library. One of the most popular Node modules is “Express”. So, let’s see how we can install express in our project.

Installing express in the project

  1. Open your terminal (or command prompt) and navigate to your project folder.
  2. Execute the following command to install express
    npm install --save express

    The –save part is necessary, cause without it npm will install express but will not record its version.
    Now if you check the package.json file in your project, you will find a property named “dependencies”, which have “express” with it’s version number in it.

  3. Now express is installed are ready to use in your project.

Using express in the project

In the last point we’ve described how to install express. Now let’s see how to use them in the program.

Step 1:

Include express in the program. The a variable named ‘app’ is holding an express app.

var express = require('express');
var app = express();

Step 2:

You can define the routes using express. Here goes an example.

app.get("/", function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World Home route");

This will create a GET request route.

Another step:

Below is an example of two GET and one POST routes.

app.get("/", function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World Home GET route \n");

app.get("/abroad", function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello Abroad GET route \n");
});"/abroad", function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World Abroad POST route \n");

Run the code

  1. Open you terminal or command prompt and navigate to your project folder.
  2. Use the following command to run
    	node index

Check the output

You can use “postman”, a Google chrome extension to check the routes. Or you can directly browse the urls http://localhost:8000/ and http://localhost:8000/abroad/ in your browser and check the results.


So this is how Node works. Basic node doesn’t provide much usable features. You have to use third party modules to make a rich app, as we have used express here. Express handles a lot more things and probably is the most popular node module at this point of time. Hope you get the basics of node and ready to dive deep into the Node world.