joyent/node — Issue #2044: cluster.fork() - Start the server in a callback function
Author: thefosk
Created: 2011-11-08 04:38:35+00:00
State: open

I wrote the following piece of code, where I start an HTTP server in a child process only when a message is received by the master. The server is unreachable.

``` javascript
if (cluster.isMaster) {
var child = cluster.fork();
child.send({type:1, options:{port:8080, key:"12345"}});
} else {
process.on("message", function (message) {
if (message.type == 1) {
http.Server(function(req, res) {
doSomething(message.options.key);
}).listen(message.options.port);
}
});
}
```