Skip to content

Instantly share code, notes, and snippets.

@listochkin
Last active December 21, 2015 15:29
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 listochkin/6327352 to your computer and use it in GitHub Desktop.
Save listochkin/6327352 to your computer and use it in GitHub Desktop.
Simple domain example
var server = http.createServer(function(req, res) {
var requestDomain = domain.create();
var sentError = false;
requestDomain.on('error', function(error) {
console.error('Request error', error.stack);
try {
server.close();
shutdown();
if (!sentError) {
sentError = true;
res.statusCode = 500;
res.setHeader('content-type', 'text/plain');
res.end('Oops, there was a problem!\n');
}
} catch (error2) {
console.error('Error sending 500!', error2.stack);
}
});
requestDomain.add(req);
requestDomain.add(res);
requestDomain.run(function() {
switch(req.url) {
case '/error':
// We do some async stuff, and then...
setTimeout(function() {
console.log(requestDomain === domain.active && requestDomain !== null);
// Whoops!
flerb.bark();
});
break;
default:
res.end('ok');
}
});
});
server.listen(1337);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment