在我的小node.js应用程序中,使用express,我想记录所有传入的请求,所以我最终得到了这个:
var bodyParser = require('body-parser');
module.exports = function(app) {
app.set("port", 50001);
app.set("json spaces", 2);
app.use(bodyParser.json());
app.use(function (error, req, res, next) {
app.logger.info("received from "+req.get("X-Forwarded-For")+" : "+req.method+" "+req.originalUrl+" (Authorization: "+req.get("Authorization")+")");
//does not work if json is malformed
//app.logger.info("content :"+JSON.stringify(req.body));
if (error /*instanceof SyntaxError*/) {
res.status(400);
app.logger.error(error);
res.json({ error:{msg: error.message}});
} else {
next();
}
});
app.use(app.auth.initialize());
};
Run Code Online (Sandbox Code Playgroud)
不幸的是,我只是app.logger.info在出现错误时通过线路获取日志(在我的情况下,正文中的格式错误的JSON字符串).我在这里错过了什么?