node.js - 使用NodeJS和Express显示在哪里抛出错误500

标签 node.js express error-handling

我正在使用节点,并且尝试将显示重定向为404和500错误。

现在我有这段代码可以正常工作:

/* ... Some require here ... */

var app = module.exports = express();

/* ... Some app config here */

// Setup my routes
routes.setup(app);

// Handling 404 errors
app.use(function(req, res) {
   res.end("Oups !", 404);
});

// Handling 500 errors
app.use(function(error, req, res, next) {
   console.log("Display error : ", error);
   res.end("Oups !", 500);
});

// Start server
var server = app.listen(config.PORT);

现在,如果在我的代码上执行throw new Error("An error here");在console.log上,我刚得到一个字符串:
Display error : An error here

而且,如果我删除了用于处理错误500的部件,那么在控制台日志上,我会自动获得错误来源的说明:
Error: An error here
    at Layer.handle (/.../app.js:94:7)
    at trim_prefix (/.../node_modules/express/lib/router/index.js:226:17)
    at c (/.../node_modules/express/lib/router/index.js:198:9)
    at Function.proto.process_params (/.../node_modules/express/lib/router/index.js:251:12)
    at next (/.../node_modules/express/lib/router/index.js:189:19)
    at next (/.../node_modules/express/lib/router/index.js:166:38)
    at next (/.../node_modules/express/lib/router/index.js:166:38)
    at next (/.../node_modules/express/lib/router/index.js:166:38)
    at next (/.../node_modules/express/lib/router/index.js:166:38)
    at Layer.handle (/.../app.js:50:5)

我如何获得有关向何处射击的信息?

谢谢大家!

最佳答案

/ Setup my routes
routes.setup(app);

可能是您在route.setup(app)中有问题;

首先删除行代码测试您的应用程序

关于node.js - 使用NodeJS和Express显示在哪里抛出错误500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28732287/

相关文章:

reactjs - app.delete 中的 req.params 为空,在 react 中使用 express、axios

rust - 应用程序的错误处理 : how to return a public message error instead of all the chain of errors and tracing it at the same time?

node.js - 如何使用 cron 作业将资源发送给我的客户

node.js - 如何使用 nodejs 在 Linux 中创建新用户

javascript - 2/4 Angular handle 资源下载

node.js - 快速静态目录如何与 404 路由一起使用?

php - PHP : Blank page, error reporting not functioning

python - 带有 Morningstar 的 Pandas Datareader

node.js - 终止 Faye 客户端连接

node.js - 在使用 Webpack 构建 Aurelia 应用程序期间找不到命名空间 'Webpack'