Created
September 19, 2014 09:40
-
-
Save ishiduca/0e505391e48747fdcc4b to your computer and use it in GitHub Desktop.
use Domain in 'carom.js app'
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 app = Object.create(require('carom.js')).constructor() | |
function onError (err, req, res) { | |
console.error(err) | |
console.error(err.stack) | |
res.writeHead(500) | |
res.end(String(err)) | |
} | |
app.use('favicon') | |
app.use('doma', onError) | |
app.use('router', {app: app}) | |
app.use('404') | |
app.router.GET('/', function (req, res) { | |
throw new Error('test throw error !!') | |
}) | |
app.router.GET('/domain', function (req, res) { | |
var domain = this.d | |
require('fs').readFile('no_exists.json', 'utf8', domain.intercept(function (data) { | |
res.writeHead(200,{'content-type': 'application/json; charset=utf-8'}) | |
res.end(data) | |
})) | |
}) | |
var port = 3002 | |
app.server.listen(port, function (err) { | |
if (err) return console.error(err.stack) | |
console.log('server start to listen on port "%d"', port) | |
}) |
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
module.exports = function (onError) { | |
if ('function' !== typeof onError) | |
throw new TypeError('"onError" must be "function"') | |
var domain = require('domain') | |
return function errorhandling (req, res, next) { | |
var context = this | |
var d = this.d = domain.create() | |
d.on('error', function (err) { | |
onError.apply(context, [err, req, res]) | |
}) | |
d.run(next) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment