当我的代码有错误时,如何从 restify 获取堆栈跟踪?

How can I get stack traces from restify when I have errors in my code?

假设我定义了以下 restify 端点:

server.get('/test/1', function (req, res, next) {
    undefinedFunctionCall(); // Will cause exception
    return next();
}

如何在我的服务器控制台中获取堆栈跟踪,告诉我 undefinedFunctionCall 未定义? 客户端 获取它但不是服务器。

我在the docs读到可以监听事件InternalServerErrorInternalError,但是none我测试的时候火了。我必须做什么?

代码:

server.on('InternalServerError', function (req, res, err, cb) {
    console.log('INTERNAL ERROR!'); // Never executed
    return cb();
});

我 运行 在 Windows 10

节点 0.10.33 上 Restify 4.0.0

嗯,这似乎随着 4.0.0 版发生了变化。我还在使用 0.10 版本,堆栈跟踪自动输出到服务器控制台。

但是,试试我从 this post 中收集到的这个解决方案。我试过了,对我有用。

server.on('uncaughtException', function (req, res, err, cb) {
    console.log(err);
    return cb();
});

这与服务器死机并不完全相同,但至少您得到了堆栈跟踪。