Skip to content

Instantly share code, notes, and snippets.

@gu-stav
Created September 4, 2013 06:15
Show Gist options
  • Select an option

  • Save gu-stav/6433315 to your computer and use it in GitHub Desktop.

Select an option

Save gu-stav/6433315 to your computer and use it in GitHub Desktop.
nodejs - domains. Taken from http://nodetuts.com/08-domains.html
var EventEmitter = require('events').EventEmitter;
var domain = require('domain');
var fs = require('fs');
var a;
var server = require('http').createServer();
server.on('request', function(req, res) {
var d = domain.create();
d.add(req);
d.add(res);
var replied = false;
d.on('error', function(err) {
if (! replied) {
replied = true;
res.writeHead(500);
res.end(err.message);
}
});
d.run(function() {
fs.readFile(__filename, d.intercept(function(contents) {
res.end(contents);
}));
});
});
server.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment