Server to Server communication in NodeJS
Posted By : Aftab Alam | 24-Apr-2018
1. Introduction:- When any url is typed in broswer's url bar then this request is handled by a server. This interaction between browser and server is referred as client-server communication. In the same way, when a server wants to talk to another server then is called server-to-server communication.
2. Prerequisite for server-to-server communication:- Prerequisite are listed below.
2.1. Basic understaning of nodeJS
2.2. http module of nodeJS
3. NodeJS http Server creation:-
var http=require("http");
var httpServer=http.createServer(function(req, res){
});
httpServer.listen(8089, function(){
var port=httpServer.address().port;
var address=httpServer.address().address;
console.log("Server is running at http://" ,address ,":", port);
});
In the first line, a nodeJS http module is imported which, returns an object, is used to create http server.
In the second line, an http server is created, but not started yet, using createServer function of http object which receives a call back function, takes req and res two arguments, would be invoking on each request coming from the server.
In the third line, httpServer object's listen function takes two arguments first one as port number and second one is a callback function which is invoked on successful runing of the server.
4. NodeJS http Server Creation for handling post request:-
var http=require("http"); var httpServer=http.createServer(function(req, res){ var str=''; if(req.method=="POST"){ req.on("data", function(chunk){ str=str+chunk; }); req.on("end", function(){ res.end(str); }); } }); httpServer.listen(8089, function(){ var port=httpServer.address().port; var address=httpServer.address().address; console.log("Server is running at http://" ,address ,":", port); });
In side callback function, if requested method is POST then data is read coming in request and same data is retuned to requested server as response.
5. NodeJS server for making a post request to above server:-
var http=require("http"); var httpServer=http.createServer(function(req, res){ var options={ host:"0.0.0.0", port:8089 }; var data="Sultan Husain Chaudhary"; options.method="POST"; options.headers={ "Content-Type":"application/x-www-form-urlencoded", "Content-Length":Buffer.byteLength(data) }; serverToServerRequest(options, data, res); }); httpServer.listen(8084, function(){ port=httpServer.address().port; address=httpServer.address().address; console.log("Server is running at http://" ,address ,":", port); }); function serverToServerRequest(options, data, res){ var clientRequest=http.request(options, function(response){ console.log("Received a request"); var str=''; response.on("data", function(chunk){ str=str+chunk; }); response.on("end", function(){ console.log("Received Data: ", str); res.end("Hello: "+str); }); }); clientRequest.on("error", function(error){ console.log("Error Occured:--", error); }); clientRequest.write(data); clientRequest.end(); }
In the same way, the server is created except it is running on different port number that is 8084.
When any request comes to server then it creates options to make a request to the server that is running on port number 8089.
In serverToServerRequest function, a function of http object is used to make server to server interaction. It takes two arguments first one as option and second one as callback function that would be called on successful response from the requested server.
function(response){ console.log("Received a request"); var str=''; response.on("data", function(chunk){ str=str+chunk; }); response.on("end", function(){ console.log("Received Data: ", str); res.end("Hello: "+str); }); }
In this callback function, data event is triggered to get data from the server and at last chunk of data being send by server an end event is triggered that sends this data to the server.
clientRequest.on("error", function(error){ console.log("Error Occured:--", error); });
This is used to handle error while one server is interacting with another server.
clientRequest.write(data);
This is used to write data to out-going request.
clientRequest.end();
This sends send the request to the server.
6. Run and Test:-
6.1. npm install http
6.2. Save first server code in server1.js and run as node server1.js
6.3. Save second server code as server2 and run as node server2.js
6.4. Open postman and type http://locallhost:8084 with POST method.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aftab Alam
Aftab has worked on multiple technologies in front-end as well as in back-end.