Skip to content

Instantly share code, notes, and snippets.

@fengmk2
Last active December 14, 2015 11:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fengmk2/5079492 to your computer and use it in GitHub Desktop.
Save fengmk2/5079492 to your computer and use it in GitHub Desktop.
var domain = require('domain');
var http = require('http');
var domainMiddleware = function (req, res, next, errorHandle) {
var d = domain.create();
d.once('error', errorHandle);
d.run(next);
};
var app = http.createServer(function (req, res) {
domainMiddleware(req, res, function () {
// normal response
if (req.url === '/error') {
process.nextTick(function () {
var a = null;
a.foo();
});
return;
}
res.end('hello domain');
}, function (err) {
// sending err response
res.statusCode = 500;
res.end(err.stack);
});
});
app.listen(1984);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment