【发布时间】:2013-07-17 18:48:18
【问题描述】:
Node.js 中的错误处理。啊!
我正在尝试像这样布局一个基本的 Node 应用程序......
Cluster -> Worker -> Server Domain -> Express Request Domain
因此,如果由于有人在登录表单上拼错了自己的姓名而将错误抛出到调用堆栈的 18 层深处,则整个服务器不会崩溃。
这里有一些基本的代码来模拟工人部分:
var domain, server;
domain = require('domain');
server = domain.create();
server.on('error', function(e) {
console.log('total meltdown...', e.stack);
});
server.run(function() {
var express = require('express')();
express.configure(function() {
// Domain on EVERY request
express.use(function(req, res, next) {
var d = domain.create();
d.on('error', function(e) {
console.log('fired REQUEST error', e.stack);
next(e);
});
d.run(next);
});
// Generic error handler
express.use(function(e, req, res, next) {
res.status(500);
res.end('oops');
});
// Serve the request with a blatent error
express.get('/', function(req, res) {
this_function_does_not_exist();
res.end('we will never get here');
});
});
// Fire 'er up
express.listen(3000);
});
我所期待的......
我 curl http://localhost:3000/,得到一个不错的小“哎呀”错误,并在控制台中看到“触发请求错误”和错误堆栈。
实际发生了什么......
我在浏览器和控制台中都得到了这个响应......
ReferenceError: this_function_does_not_exist 未定义 在 /Stuff/test.js:38:13 在回调(/Stuff/node_modules/express/lib/router/index.js:161:37) 在参数处(/Stuff/node_modules/express/lib/router/index.js:135:11) 通过时(/Stuff/node_modules/express/lib/router/index.js:142:5) 在 Router._dispatch (/Stuff/node_modules/express/lib/router/index.js:170:5) 在 Object.router (/Stuff/node_modules/express/lib/router/index.js:33:10) 在下一个(/Stuff/node_modules/express/node_modules/connect/lib/proto.js:190:15) 在下一个(/Stuff/node_modules/express/node_modules/connect/lib/proto.js:192:9) 在 b (domain.js:183:18) 在 Domain.run (domain.js:123:23)
现在为什么它会去做那样的事情?
【问题讨论】:
标签: javascript node.js express connect npm