In this short article, let me tell you about How to Clustered HTTP Server in Node.js. Now this is the code that I ued to clustere the HTTP Server:

function () {
    'use strict';
        var cluster = require('cluster'),
        http = require('http'),
        os = require('os'),       
        /*
         * ClusterServer object
         *
         * We start multi-threaded server instances by passing the server object
         * to ClusterServer.start(server, port).
         *
         * Servers are automatically started with a number of threads equivalent
         * to the number of CPUs reported by the os module.
         */
        ClusterServer = {
            name: 'ClusterServer',           
            cpus: os.cpus().length,           
            autoRestart: true, // Restart threads on death?
                        start: function (server, port) {
                var me = this,
                    i;                
                if (cluster.isMaster) { // fork worker threads
                    for (i = 0; i < me.cpus; i += 1) {
                        console.log(me.name + ': starting worker thread #' + i);
                        cluster.fork();
                    }                   
                    cluster.on('death', function (worker) {
                        console.log(me.name + ': worker ' + worker.pid + ' died.');
                        if (me.autoRestart) {
                            console.log(me.name + ': Restarting worker thread...');
                            cluster.fork();
                        }
                    });
                } else {
                    server.listen(port);
                }
            }
       },       
        /*
         * Simple example HelloWorld HTTP server
         *
         * Repsonds to any request with a plain txt "Hello World!" message.
         *
         * You can replace this with much more complex processing, naturally.
         */
        helloWorldServer = http.createServer(function (request, response) {
            response.writeHead(200, {
                'Content-type': 'text/plain'
            });
                        response.end('Hello World!');
            console.log('helloWorldServer: Served a hello!');
        });   
    ClusterServer.name = 'helloWorldServer'; // rename ClusterServer instance    ClusterServer.start(helloWorldServer, 8081); // Start it up!
}());

I hope it works for you!

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.