Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
});
}
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();
});
}
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
You can’t perform that action at this time.