Spread The Knowledge

How To Begin With Node Js

Mahesh Dhaka CEO at WEBHOBS

Node.js is an open source Javascript runtime environment for server-side and network applications. It brings event-driven programming to the web servers with a non-blocking I/O API that optimizes an application's throughput and scalability. Let's learn to work with Node.js:-

How To Install Node.js On Windows

1. Download the setup file from Node JS Official Website

2. Run the installer and accept the license agreement.

3. After finishing up with the setup, let's run few of the commands to test whether things working fine or not.

Begin The Test

Open the command line and perform following tasks:-

Node Test: Type node -v at the command line and the result should be the version number(In my case it's v7.7.1).

NPM Test: Type npm -v at the command line and the result should be the NPM’s version number(In my case it's 4.1.2).

Let's Run the Hello World In Node.js

1. Create a javascript file myfirstprogram.js with following code and save it to C:\\Users\username\


console.log("Hello World");

2. Run the command node myfirstprogram.js and the output will be:-


Hello World

Let's run the code at browser by creating the Server

1. Load the http module and store the returned instance into an variable.


var http1 = require("http");

2. Create the server by using the http instance http1.createServer() and bind it to 8081 port.


var http1 = require("http");
http1.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');
}).listen(8081);

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

3. Open the browser and type 127.0.0.1:8081 and the result will be:-


Hello World


I have listed few of the basic steps to work with Node.js. If you are facing any difficulties throughout the installation or execution, can let me know through comments

Comment Section

The Smackdown b/w Node JS V/S PHP

5 Reasons Why Node Js is Best Platform For The Web Development

How To Begin With Node Js