Saturday 3 June 2017

Do all AWS Node.js apps require the cluster module?

I was following the tutorial here, and noticed the index file (app.js) of the example code is quite different than a usual Express.js application. The code is wrapped inside the cluster module as follows:

// Include the cluster module
var cluster = require('cluster');

// Code to run if we're in the master process
if (cluster.isMaster) {

    // Count the machine's CPUs
    var cpuCount = require('os').cpus().length;

    // Create a worker for each CPU
    for (var i = 0; i < cpuCount; i += 1) {
        cluster.fork();
    }

    // Listen for terminating workers
    cluster.on('exit', function (worker) {

        // Replace the terminated workers
        console.log('Worker ' + worker.id + ' died :(');
        cluster.fork();

    });

// Code to run if we're in a worker process
} else {
    var AWS = require('aws-sdk');
    var express = require('express');
    var bodyParser = require('body-parser');
    // the usual code ..
}

Is this necessary, or can I just deploy the regular code without the cluster module, like this:

var AWS = require('aws-sdk');
var express = require('express');
var bodyParser = require('body-parser');
// the usual code ..

Thanks,



via jeff

No comments:

Post a Comment