Created
June 13, 2012 00:13
-
-
Save isaacs/2920955 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var net = require('net'); | |
if (process.argv[2] === 'child') child(); | |
else parent(); | |
function child() { | |
// start a server on fd=3 | |
http.createServer(function(req, res) { | |
console.error('request on child'); | |
console.error('%s %s', req.method, req.url, req.headers); | |
res.end('hello from child\n'); | |
}).listen({ fd: 3 }, function() { | |
console.error('child listening on fd=3'); | |
}); | |
} | |
function parent() { | |
var server = net.createServer(function(conn) { | |
console.error('connection on parent'); | |
conn.end('hello from parent\n'); | |
}).listen(8000, function() { | |
console.error('server listening on 8000', server._handle); | |
var spawn = require('child_process').spawn; | |
var child = spawn(process.execPath, [__filename, 'child'], { | |
stdio: [ 0, 1, 2, server._handle ], | |
detached: true | |
}); | |
console.error('child pid = %d', child.pid); | |
// Now close the parent, so that the child is the only thing | |
// referencing that handle. Note that connections will still | |
// be accepted, because the child has the fd open, but the parent | |
// will exit gracefully. | |
server.close(); | |
child.unref(); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var net = require('net'); | |
if (process.argv[2] === 'child') child(); | |
else parent(); | |
function child() { | |
// start a server on fd=3 | |
http.createServer(function(req, res) { | |
console.error('request on child'); | |
console.error('%s %s', req.method, req.url, req.headers); | |
res.end('hello from child\n'); | |
}).listen({ fd: 3 }, function() { | |
console.error('child listening on fd=3'); | |
}); | |
} | |
function parent() { | |
var server = net.createServer(function(conn) { | |
console.error('connection on parent'); | |
conn.end('hello from parent\n'); | |
}).listen(8000, function() { | |
console.error('server listening on 8000', server._handle); | |
var spawn = require('child_process').spawn; | |
var child = spawn(process.execPath, [__filename, 'child'], { | |
stdio: [ 'ignore', 'ignore', 'ignore', server._handle ], | |
detached: true | |
}); | |
console.error('child pid = %d', child.pid); | |
// Now close the parent, so that the child is the only thing | |
// referencing that handle. Note that connections will still | |
// be accepted, because the child has the fd open, but the parent | |
// will exit gracefully. | |
server.close(); | |
child.unref(); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var net = require('net'); | |
if (process.argv[2] === 'child') child(); | |
else parent(); | |
function child() { | |
// start a server on fd=3 | |
http.createServer(function(req, res) { | |
console.error('request on child'); | |
console.error('%s %s', req.method, req.url, req.headers); | |
res.end('hello from child\n'); | |
}).listen({ fd: 3 }, function() { | |
console.error('child listening on fd=3'); | |
}); | |
} | |
function parent() { | |
var server = net.createServer(function(conn) { | |
console.error('connection on parent'); | |
conn.end('hello from parent\n'); | |
}).listen(8000, function() { | |
console.error('server listening on 8000', server._handle); | |
var spawn = require('child_process').spawn; | |
var child = spawn(process.execPath, [__filename, 'child'], { | |
stdio: [ 0, 1, 2, server._handle ] | |
}); | |
child.on('exit', function(code) { | |
console.error('child exited', code); | |
}); | |
child.on('close', function() { | |
console.error('child closed'); | |
}); | |
console.error('child spawned'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment