![clock](../themes/indigo/img/timeicon.gif)
March 17, 2015 06:44 by
Peter
In this tutorial , I will discuss about Cluster Server in NodeJS. That is a group of two or more computers that work together to provide availability, reliability and scalability that can be obtained using a single computer. In a server cluster, each server owns and manages devices and applications or services that are managing local cluster.
![](http://fulltrusteuropeanhosting.hostforlife.eu/image.axd?picture=2015%2f2%2fhostforlife+banner-01.png)
To make the new Node Web Application open Visual Studio then select "File" -> "New Web Site" then select "Node Web Application" as in the accompanying figure:
![](http://fulltrusteuropeanhosting.hostforlife.eu/image.axd?picture=2015%2f3%2fiis-1.jpg)
Create a new Script file from the Solution Explorer as in the following figure:
![](http://fulltrusteuropeanhosting.hostforlife.eu/image.axd?picture=2015%2f3%2f2.jpg)
And then select the new JavaScript file as in the following figure:
![](http://fulltrusteuropeanhosting.hostforlife.eu/image.axd?picture=2015%2f3%2f3.jpg)
Write the following code in a JavaScript File.
(function () {
'';
var cls = require('cluster'),
http = require('http'),
os = require('os'),
ClsServer = {
clusname: 'ClusterServer',
cpus: os.cpus().length,
autoRestart: true,
start: function (server, port) {
var me = this,
i;
if (cls.isMaster) {
for (i = 0; i < me.cpus; i += 1) {
console.log(me.name + ': starting worker thread #' + i);
cls.fork();
}
/*cls.on('death', function (worker) {
console.log(me.clusname + ': worker ' + worker.pid + ' died.');
if (me.autoRestart) {
console.log(me.clusname + ':thread restart');
cls.fork();
}
});*/
} else {
server.listen(port);
}
}
}
helloWorldServer = http.createServer(function (request, response) {
response.writeHead(200, {
'Content-type': 'text/plain'
});
response.end('Hello World!');
});
ClsServer.name = 'hello World Server';
ClsServer.start(helloWorldServer, 8081);
}());
In the above case we make a ClusterServer article to begin multi-strung server examples by passing the server item to ClusterServer.start(server,port). Servers are naturally begun with various strings proportional to the quantities of CPUs reported by the os module.Debug the application by pressing F5 and the output will be demonstrated in a reassure application as in the accompanying figure:
![](http://fulltrusteuropeanhosting.hostforlife.eu/image.axd?picture=2015%2f3%2f4.jpg)
HostForLIFE.eu Node.js Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.
![](http://iis75europeanhosting.hostforlife.eu/image.axd?picture=2015%2f1%2frecommended+windows+hosting.png)