Created
June 6, 2013 19:15
-
-
Save gjohnson/5724104 to your computer and use it in GitHub Desktop.
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
| /** | |
| * Dependencies. | |
| */ | |
| var Domain = require('domain'); | |
| var http = require('http'); | |
| var app = require('..'); | |
| /** | |
| * Export `server`. | |
| */ | |
| var server = http.createServer(onRequest); | |
| /** | |
| * Listen. | |
| */ | |
| server.listen(process.env.PORT || 3000); | |
| /** | |
| * Request handler. | |
| * | |
| * Wraps `req` and `res` with a domain and forwards the request along to `app`. | |
| * | |
| * Note: If any strange errors happen or there is a sign of memory leaks, I | |
| * would start here first as domains are pretty new. | |
| */ | |
| function onRequest(req, res){ | |
| var domain = Domain.create(); | |
| domain.on('error', function(error){ | |
| console.error(error.stack); | |
| try { | |
| var reaper = setTimeout(function(){ | |
| process.exit(1); | |
| }, 10000); | |
| reaper.unref(); | |
| server.close(); | |
| res.statusCode = 500; | |
| res.end(error.message); | |
| } catch (error) { | |
| console.error(error.stack); | |
| } | |
| }); | |
| domain.add(req); | |
| domain.add(res); | |
| domain.run(function(){ | |
| app(req, res); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment