Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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