我想要创建一个中间件来保存我的数据库中的日志。
但是在我下面的中间件中,任何URL都是保存的,我不想保存"favicon.ico“--例如"src of my”.我只想保存由用户触发的URL和帖子。
server.app.use(function(request, response, next) {
LogModel.create({
type: request.method,
url: response.statusCode + ' : ' + request.url
}).then(function() {
console.log(colors.green('URL : ' + request.originalUrl));
});
next();
});
结果:
预期结果:
发布于 2021-01-15 14:40:55
当然,您可以在中间件中手动检查请求路径,并通过调用next()
跳过特定路径的逻辑。另一种选择是使用像明示-除非这样的包,它允许您使用各种选项来控制中间件。在这种情况下,可以指定要跳过的路径:
const unless = require('express-unless');
const app = express();
const loggingMiddleware = (req, res, next) => {
LogModel.create(...);
next();
};
loggingMiddleware.unless = unless;
app.use(loggingMiddleware.unless({
path: [
'/favicon.ico' // add more paths if needed
]
}));
https://stackoverflow.com/questions/65737664
复制相似问题