Monday 28 May 2018

Understanding NodeJS With Simplified Way

Technology is evolving everyday and if you are not updated with it you can not survive in this highly competitive IT industry.

I still remember my first year of engineering when PHP is considered as god for web developers,But in just four years i have witnessed how rapid technologies evolves and now PHP has lot of competitors like Python(Django etc.), Ruby on Rails, NodeJS and many more.

Among all of them NodeJS is most popular and trending, NodeJS is not going to replace PHP, Rails or Python, but it has some awesome features that make it stands out of queue:

1. Uses complete Javascript for everything.
2. Very Fast as it is Based on Chrome V8 Engine .
3. Single Threaded and Non-Blocking Model.
4. At lower level it is written in C language so it its execution is very fast.


Actually what is NodeJS and why you should use it??


First of All NodeJS is not a programming language, It is just a Server Side Runtime Wrapper on Chrome V8 engine and uses Javascript as programming language, In past Javascript is considered as client side language which can be used to execute client side things like validations etc, but after the introduction of NodeJS Javascript can be used on Server side programing and you can make complete application with just using javascript. So it reduced the need of learning multiple languages for client and server programming. For building NodeJS Application we use various third party modules to implement different features, and NodeJS comes with its own popular package manager to manage different modules called NPM(Node Package Manager).

We have used the word Server a lot of time, its time to make our own server with NodeJS, that can accept requests  and return responses as we define.

To Get Started with Node JS first we need NodeJS Installed on our computer.

You Can download the LTS version of it from: https://nodejs.org/en/

Installation is quite straight forward.

As i said node uses a package manager to manage the dependency modules and all the application dependecy modules are mentioned in a file called "package.json",
So lets get started with some hands on actual implementation.

Step 1. Make a folder for you project suppose i name it "NodeJS_Starter".

Caution: Please don't be scared of black screen, you will love it.

Step 2. Open your favourite terminal(CMD,Powershell,gitBash etc),I am using gitbash, as shown in below screenshot.

Step 3. Now  write "npm init" in the terminal and it will ask some details about your application and create a "package.json" file containing all details and configuration of your project, as shown in the below screenshot. 
              

Step 4. Now you Have "package.json" file in root directory of your project.
Step 5. Now we have to Create the main Server file "index.js" which contains all the server stuffs.
Step 6: Here is the content of "index.js" for making a hello world server .
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Explanation of Code:
Line 1: we have included http module which contains various http utilities, and assigned it to a variable called "http".
Line 2 & 3: We are assigning the value of  host address and port to variables on which we want to start the server.
Line 4: we are Creating the Server with our own configuration. every request has two parameters that is "req" and "res", in other words "req" is incoming pipe and "res" is outgoing pipe.
Line 5. We are setting the status code of  response to "200".
Line 6. We are setting the type of  response contained in the response.
Line 7. We are sending "Hello World" as body content.

Line 8 & 9. We are starting the server with above Configuration and port and logging a message to the console.

Step 6: To run and Test the server  open the terminal in the project directory and run "node index" command you will see the message "Server running at http://127.0.0.1:3000/" as shown in the below screenshot.


Step 7. Now Open "http://127.0.0.1:3000/" in your web browser and see the magic Happening. it will display "Hello World" in the browser , as shown in the screen shot below:

Congratulations, You have created your first nodejs server which responds with "Hello World".

In feature Posts i will come up with some more details request and response types and using third party modules and frameworks like ExpressJS.  

0 comments: