【问题标题】:Why is log output twice using app.get() and app.use()?为什么使用 app.get() 和 app.use() 会输出两次日志?
【发布时间】:2012-08-20 08:20:45
【问题描述】:

我有一个非常基本的 Express 网络服务器:

var app = module.exports = express.createServer();
app.get('/:user', function(req, res) {
    console.log('GET');
});

app.param('user', function(req, res, next, id) {
    console.log('PARAM');
    next();
});
app.listen(3000);

当我运行http://localhost:3000/MyName 时,我的控制台中有以下输出:

PARAM
GET
PARAM
GET

为什么我每个输出都得到两次?

【问题讨论】:

    标签: javascript express


    【解决方案1】:

    浏览器很可能自己发送了第二个请求以获取网站图标 (http://localhost:3000/favicon.ico)。

    【讨论】:

    • 是的,这可能就是原因。我没想到。谢谢!
    • @Amberlamps 您可以使用“logger”中间件轻松显示所有请求:app.use(express.logger());
    • 感谢您的建议!我一定会尝试的。
    猜你喜欢
    • 2015-01-25
    • 2013-03-14
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    相关资源
    最近更新 更多