Hello friends, now a days Node is very popular for creating Server-Side applications using Javascript. So its better to learn more about this Node. Last post we discussed about Node Js introduction, hope you got some concepts regarding Node. Now in this post we are going learn following things,
- Why we need to create a separate server to run Node.Js application.
- How we create a server using Node.Js.
- How to Run the server
Why we need to create a separate server to run Node.Js application ?
As we know that to run a server-side application, we need a server. Similarly as Node.Js application is a server-side application, to run the Node.Js application we also need server.
But there is no separate server like Apache or IIS to run a Node.Js application. Node.Js itself provided some built-in module (i.e HTTP, NET module) to create a server to run our Node.Js application.
So final conclusion is that, as there is no separate server software, we need to create a separate server by using Node built-in module, to run a Node aplication.
How we create a server using Node.Js ?
As we discussed above, that Node has two built-in modules, such as http and net, by using these two modules we can implement http server as well as tcp server.
Following are the steps to create a server using node and its built-in modules,
Steps 1 : Creating Server Object i.e HTTP Object or TCP Object by using HTTP or NET module respectively.
Step 2 : Creating the Server by using above Server Object.
Step 3 : Providing the IP Address and port number for listening the Server.
Next, first we will create a HTTP Server then we will create TCP Server.
Creating HTTP Server :
For creating HTTP Server, we need http module provided by Node.
Step 1: (Creating http object)
Following is the code to create http object,
var http = require('http');
Step 2 : (Creating Server)
Above http object contains a built-in method i.e createServer() to create server. Following is the code for creating the Server,
var server = http.createServer(function(request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('This is HTTP Server\n');
});
Step 3 : (Providing Port No and IP )
Now the above server object contains a method named as listen(), by using this method we can provide the port no to the server,
server.listen(1337, '127.0.0.1');
Following is the complete code to create http server using Node.Js
var http = require('http');
var server = http.createServer(function(request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('This is HTTP Server\n');
});
server.listen(1338, '127.0.0.1');
Creating TCP Server :
For creating TCP Server, we need net module provided by Node.
Step 1: (Creating tcp object)
Following is the code to create tcp object,
var tcp = require('net');
Step 2 : (Creating Server)
Above tcp object contains a built-in method i.e createServer() to create server. Following is the code for creating the Server,
var server = net.createServer(function (socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
Step 3 : (Providing Port No and IP )
Now the above server object contains a method named as listen(), by using this method we can provide the port no to the server,
server.listen(1337, '127.0.0.1');
Following is the complete code to create http server using Node.Js
var tcp = require('net');
var server = net.createServer(function (socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(1338, '127.0.0.1');
How to Run the server ?
Its very simple the run our above http or tcp server. Just we need to provide our file name with node in command prompt. For example, if we will save the above code in server.js for any one type of server (i.e http or tcp), then we can run the server by using following command,
node server.js for windows
$node server.js for Linux
Now we can check our application is running or not, in browser by using the link i.e http://127.0.0.1/1338 (for http server).
This comment has been removed by the author.
ReplyDelete