One language to rule them all

Hey you guys, today lets go over some of the basics of Node. Node is a full stack language based on the fronted Java script. There ae some main concepts that you should understand to move forward with Node.

1. Non-blocking or Asynchronous I/O

All APIs of Node.js library are asynchronous, that is, non-blocking. It essentially means a Node.js based server never waits for an API to return data. The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call.

2. Single Threaded but Highly Scalable

Node.js uses a single threaded model with event looping. Event mechanism helps the server to respond in a non-blocking way and makes the server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and the same program can provide service to a much larger number of requests than traditional servers like Apache HTTP Server.

3. No Buffering

Node.js applications never buffer any data. These applications simply output the data in chunks.

enough with the age old introductions lets get into some code, 😉

To hit things off initially set up the environment for the project by typing in

npm init- this will initialize the package.json file with all the details you provided initially

npm init

npm-init.PNG

now that we have the package.json file let us first set up the server. below you can find the code to set the server up

initiakize-server

now the breakdown of the code is as follows

var http = require('http'),
 host = '127.0.0.1',
 port = '9990';

var server = http.createServer(function(request , response){
 response.writeHead(200, {'Content-Type' : 'text/html'});
 response.end('<h1>Hello World!</h1>');
}).listen(port, host, function(){
 console.log("server running on http:// " + host + " at " + port);
})
initially I have defined the variables for the program, we have to import the http module to use the HTTP server and the client

Then lets call createServer method in the http module which uses a callback function with parameters for the request and the response.

var server = http.createServer(function(request , response){...}

Within the body, I then set the header of result a value of 200 which is the HTTP OK. you can read more about HTTP headers in this link and the content type as ‘text/plain’ because we will be  displaying a plain text here, then the value to be returned to the request is set by the end method

 response.writeHead(200, {'Content-Type' : 'text/html'});
 response.end('<h1>Hello World!</h1>');

Next given that the the server is running on the said host with the mentioned port number we should notify the user that the service is running to do that just print out a line in the console.

var server = http.createServer(function(request , response){...
}).listen(port, host, function(){
 console.log("server running on http:// " +host + ":" +port);
})

Now. to run the server go to the working directory and

npm start
 if this gives an error check if the start tag is defined. else just run by
node helloWorld.js
"scripts": {
"start": "node helloWorld.js"
},

runnig

then open the browser with the IP and port

browser.PNG

till the next time 👻👻👻

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: