Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

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).

View app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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
Something went wrong with that request. Please try again.