Skip to content

Instantly share code, notes, and snippets.

@tanguylebarzic
Created December 18, 2012 21:25
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 tanguylebarzic/4332159 to your computer and use it in GitHub Desktop.
Save tanguylebarzic/4332159 to your computer and use it in GitHub Desktop.
Here, there is no server running at 127.0.0.2, so the call the http.request at line 12 will emit an error (that should be catched by the domain).
var domain = require('domain');
var http = require('http');
var respond = function(req, res){
var httpOptions = {
host : "127.0.0.2",
port : 50,
path : "/",
method : 'GET'
};
var req1 = http.request(httpOptions, function(err1, res1){
res.writeHead(200);
res.end('Test !.\n');
});
req1.end();
};
http.createServer(function (req, res) {
var newDomain = domain.create();
newDomain.add(req);
newDomain.add(res);
var domainError = null;
newDomain.on('dispose', function(){
console.log("newDomain.on('dispose')");
});
newDomain.on('error', function(error){
domainError = error;
console.log("newDomain.on('error')");
try {
res.writeHead(500);
res.end('Error occurred, sorry.\n');
// process.nextTick(function() {
newDomain.dispose();
// });
}
catch(err){
console.error('Error sending 500', err, req.url);
// tried our best. clean up anything remaining.
// process.nextTick(function() {
newDomain.dispose();
// });
}
});
newDomain.run(function(){
respond(req, res);
});
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment