Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to do domains...
// based on an example by @mikeal
var http = require('http')
, domain = require('domain')
module.exports = function (handler) {
var server = http.createServer(function (req, resp) {
var d = domain.create()
// add these to the domain
// explicitely so they dont
// get mixed into other domains
domain.add(resp) () {
handler(req, resp)
d.on('uncaughtException', function (e) {
// cleanup io, end streams
// don't cascade to the server
// or root domain
d.once('dispose', function () {
// give a chance for streams
// and other things to end
// gracefully - eg. remove a half
// written file, rollback a transaction
d.on('error', function (err) {
resp.statusCode = 500
if(d.listeners('error').length == 1) {
d.emit('uncaughtException', err)
return server

This comment has been minimized.

Copy link

@mikeal mikeal commented Oct 4, 2012

@izs how important is it to call dispose() in this case?

@ritch it's not a given that you should bring down the process.


This comment has been minimized.

Copy link
Owner Author

@ritch ritch commented Oct 4, 2012

True, also dispose() shouldn't be a given. Updated to wrap those in an uncaughtException event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment