Skip to content

Instantly share code, notes, and snippets.

@mape
Forked from creationix/server.js
Created June 9, 2010 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mape/432225 to your computer and use it in GitHub Desktop.
Save mape/432225 to your computer and use it in GitHub Desktop.
var child_process = require('child_process'),
sys = require('sys'),
net = require('net'),
netBinding = process.binding('net');
var fd = netBinding.socket('tcp4');
netBinding.bind(fd, 8080);
netBinding.listen(fd, 128);
for (var i = 0; i < 4; i++) {
// Create an unnamed unix socket to pass the fd to the child.
var fds = netBinding.socketpair();
// Spawn the child process
var child = child_process.spawn(
process.argv[0],
[__dirname + '/worker.js'],
undefined,
[fds[1], -1, -1]
);
// For some reason stdin isn't getting set, patch it externally
if (!child.stdin) {
child.stdin = new net.Stream(fds[0], 'unix');
}
child.stdin.write(sys.inspect(fd), "ascii", fd);
// Echo output from child
child.stderr.addListener('data', function (data) {
process.stdout.write(data);
});
}
var net = require('net'),
http = require('http'),
sys = require('sys');
sys.debug("Child started " + process.pid);
// var stdin = process.openStdin();
var stdin = new net.Stream(0, 'unix');
stdin.addListener('fd', function(fd) {
http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("Hello from " + process.pid);
}).listenFD(fd, "tcp4");
});
stdin.resume();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment