Last active
December 21, 2015 15:29
-
-
Save listochkin/6327352 to your computer and use it in GitHub Desktop.
Simple domain example
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 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