gpt4 book ai didi

node.js - 如何表达.logger()除了GET/favicon.ico之外的所有请求? (Nodejs/expressjs)

转载 作者:太空宇宙 更新时间:2023-11-04 03:10:30 25 4
gpt4 key购买 nike

我想记录除 favicon.ico 之外的所有请求。起初我想简单地创建一个函数并调用express.logger(),但是在函数内部调用express.logger()不起作用。

//DOES NOT LOG
app.use("/", function(req, res, next){
console.log('executing 1');
express.logger();
next();
});

因此我无法使用 if 语句来检查 res.url。

现在我正在尝试以下操作,但我陷入困境:

app.use(function(req, res, next){
if(req.url=="/favicon.ico"){
//Somehow skip the next app.use
}else{
next(); //otherwise just go to next
}
});

app.use("/", express.logger());

非常感谢!

最佳答案

express.logger() 函数返回另一个具有 Express 中间件签名的函数 (function(req, res, next))。调用它不会进行日志记录,而只是返回记录器中间件。

试试这个 -

app.use(function(req, res, next){
if(req.url=="/favicon.ico"){
next();
}else{
express.logger()(req, res, next);
}
});

从代码中删除 app.use(express.logger())。

关于node.js - 如何表达.logger()除了GET/favicon.ico之外的所有请求? (Nodejs/expressjs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21928172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com