Node.js NodeJS、Express和基于Pug的应用程序的Pug错误处理
我正在使用Node.js、Express和Pug创建一个简单的Web服务器 package.json:Node.js NodeJS、Express和基于Pug的应用程序的Pug错误处理,node.js,express,error-handling,pug,Node.js,Express,Error Handling,Pug,我正在使用Node.js、Express和Pug创建一个简单的Web服务器 package.json: { "name": "testing", "version": "0.0.7", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "body-parser": "~1.17.1", "command-line-args": "^4.0.6",
{
"name": "testing",
"version": "0.0.7",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.17.1",
"command-line-args": "^4.0.6",
"cookie-parser": "~1.4.3",
"debug": "~2.6.3",
"express": "~4.15.2",
"less-middleware": "~2.2.0",
"merge": "^1.2.0",
"morgan": "~1.8.1",
"pug": "^2.0.0-rc.2",
"request": "^2.81.0",
"serve-favicon": "~2.4.2",
"sprintf-js": "^1.1.1",
"tracer": "^0.8.9"
}
}
使用start命令启动服务器:
npm start
当应用程序包含调用不存在的pug mixin等错误时:
.container
+hossa
错误文本将呈现并显示在客户端浏览器中,如下所示:
/Projects/.../status.pug:2 1| .container > 2| +hossa 3| 4| 5| div.page.container.hidden#status(text="Status") pug_mixins.hossa is not a function
这是可以的,但可以改进
主要问题是:除了500代码之外,我在日志输出中没有看到任何错误
问题是:
- 需要哪些代码、模块和/或启动选项来记录错误,包括服务器端的完整堆栈信息
- 我如何管理它来呈现一些更用户友好的消息
app.use(function(err, req, res, next) {
// very basic!
console.error(err.stack);
// show user some message - for Ajax requests which expects a specific format
res.send(JSON.stringify({ok: false, message: err.message}));
// OR
// more advanced:
// check error type, context etc. and act accordingly
// - log message
// - send appropriaty error page etc.
// - eventually use next handler
// - set res.status
// sth. like this
if (req.xhr) {
res.status(500).send({ error: 'Something failed!' });
} else {
next(err);
}
});
你必须使用。