Last active
December 14, 2015 11:29
-
-
Save fengmk2/5079492 to your computer and use it in GitHub Desktop.
Example for using `domain` module. http://nodejs.org/docs/v0.8.21/api/domain.html#domain_domain
This file contains hidden or 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 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