Created
December 18, 2012 21:25
-
-
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).
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 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