I am trying to create a multiprocess application but I am having problems with NodeJS clustering api. Now it is very simple, however I'm not able to make it work. This is my code. Clustering.js:
var cluster = require('cluster');
var port = 8101;
if (cluster.isMaster) {
var startChooser = function (chooserId) {
cluster.fork({WorkerName: chooserId, port: port});
port++;
}
}
else {
console.log(process.env.WorkerName);
}
module.exports.startChooser = startChooser
This is my main to do simple tests:
var clustering = require('./clustering');
clustering.startChooser('12345');
The problem is that it is showing this error:
/usr/local/bin/node /Users/../../main.js
12345
/Users/../../main.js:38
clustering.startChooser('12345');
^
TypeError: clustering.startChooser is not a function
at Object.<anonymous> (/Users/../../main.js:38:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
So, apparently it is working because it is printing the workerName but it is showing that error.
module.exports = { startChooser: startChooser }The error is telling you that startChooser is not defined properly. And you are only definingstartChooserif a condition is met, this needs a refactor.