Node JS introduction

This article covers some of the very basic Node JS features and its applications. Developers who are visiting this article for first the time, let me tell you that Node JS is not for beginners.

Pre-requisites 

  1. JavaScript
  2. HTML
  3. Knowledge of any server side programming language (optional)

Introduction

Node JS allows you to build scalable network applications using JavaScript on the server side

What can you build using Node?

  1. Chat server
  2. File upload
  3. Ad server
  4. Any real time data app

About Node

To put it in simple words, Node JS is merely a wrapper that sits on top of Google Chrome’s v8 JavaScript engine that runs natively on any server. It is not a framework as many developers would reckon. There are several web frameworks built on top of node which we will cover in upcoming tutorials.

Node JS is very easy to learn.

Install

Node JS is very easy to install. Visit Node JS website where you can find easy steps to install Node JS on your system. To verify your installation open command prompt or terminal and type:

node --version

To verify NPM installation type:

npm --version

NPM or Node Package Manager manages all projects and frameworks built in Node JS. We will learn more about it in our Hello World app.

Hello World!

Let’s write our first app in Node.

To start with, we will first create a folder named NodeApps in our system’s home location. You can choose any directory or partition of your choice to create the folder. For me, I am going to create the folder under Documents/Training folder.

screen-shot-2016-11-12-at-10-13-49-pm

Under this folder we will create another folder named HelloWorld.

Navigate to the folder and type the following command:

npm init

The npm init command places a package.json file under your project folder. This file is of great importance for packaging and hosting NPM projects to NPM.

Once you type the command you will be asked to enter the project name, version and other relavant details of the project. If you wish to skip a detail, simply press enter.

screen-shot-2016-11-12-at-10-23-57-pm

Create an app.js file where you will write your first code.

We will create a node server which will run on localhost and port 8080. Once we have successfully created a server, we will print “Hello World” response to the requests that it receives from the browser.

To create a server we will a built in node package called “http”. In order to use it, we will “require” http package in our code as follows:

let http = require("http");

The http package returns an http wrapper with methods to create servers and manage http requests and responses. Using this object we will create a server to listen at port 8080:

http.createServer(function (request, response) {
     ...
}).listen(8080);

To print “Hello World” we have to simply add following lines of code inside the callback function and we are done:

response.writeHead(200);
response.end("Hello World");

The end method closes the request and sends the string “Hello World” to the browser as plain/text.

Yes! It’s that simple…

The combined code looks like something below:

let http = require("http");
http.createServer(function (request, response) {
     response.writeHead(200);
     response.end("Hello World");
}).listen(8080);

To run node server go back to your terminal or cmd and type

node app

Now, go to your browser and type http://localhost:8080

screen-shot-2016-11-12-at-10-53-11-pm

With this we come to end of our first chapter. In next chapter we will learn how to serve html pages using node server.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s