简体   繁体   English

Express 中间件被多次调用

[英]Express middleware gets called multiple times

My middlewares are getting called multiple times and I can't figure out why.我的中间件被多次调用,我不知道为什么。 It's a really short code and it is very frustrating as I just started learning express and node.这是一个非常短的代码,当我刚开始学习 express 和 node 时,这非常令人沮丧。 I don't understand why it's even getting into the second middleware, I didn't use next() , I used res.send() .我不明白为什么它甚至进入第二个中间件,我没有使用next() ,我使用res.send()

I am taking an online course and it's the same code as it is described.我正在参加在线课程,它与描述的代码相同。 I also searched stackoverflow but nothing helped.我还搜索了 stackoverflow,但没有任何帮助。 I did read something about the favicon that calls it a second time, but I can't figure out why this is getting called multiple times.我确实读过一些关于第二次调用它的 favicon 的内容,但我不明白为什么它会被多次调用。

const express = require("express");
const app = express();

app.use("/", (req, res, next) => {
    console.log("This always runs!");
    next();
});

app.use("/add-product", (req, res, next) => {
    console.log("In first middleware!");
    res.send("<h1>Add Product</h1>");
});

app.use("/", (req, res, next) => {
    console.log("In second middleware!");
    res.send("<h1>Hello from express!</h1>");
});

app.listen(3000);

If I'm opening localhost:3000/add-product I should get in the console:如果我打开localhost:3000/add-product我应该进入控制台:

This always runs!
In first middleware!

but I actually get:但我实际上得到:

This always runs!
In first middleware!
This always runs!
In second middleware!
This always runs!
In first middleware!

Could it be that the favicon automatically executes all middlewares once?会不会是favicon自动执行所有中间件一次? I added this code before the first app.use() call:我在第一个app.use()调用之前添加了这段代码:

app.get("/favicon.ico", (req, res) => res.status(204));

Now I get现在我得到

This always runs!
In first middleware!
This always runs!
In first Middleware!

I still get it twice.我仍然得到它两次。

edit edit edit:编辑编辑编辑:

This appears only to happen in chrome.这似乎只发生在 chrome 中。

Don't use app.use for routes that's mainly used for middleware registration you want to use the router.不要将app.use用于主要用于要使用路由器的中间件注册的路由。 https://expressjs.com/en/4x/api.html#app.use https://expressjs.com/en/4x/api.html#app.use

app.(post|get|delete|put)("route", function(req,res,next){})

in your case it's best to see if your browser is requesting 2 http calls.在您的情况下,最好查看您的浏览器是否正在请求 2 个 http 调用。 If so it'll double up.如果是这样,它会翻倍。

The browser is sending a request for a favicon.浏览器正在发送对网站图标的请求。 Press F12 on chrome and you will see two requests being made.在 chrome 上按 F12,您将看到正在发出两个请求。 One of them will be local host and other one will be favicon.ico.其中之一将是本地主机,另一个将是 favicon.ico。 Therefore you are seeing two sets of console.log() being printed out to the console.因此,您会看到两组 console.log() 被打印到控制台。

在此处输入图片说明

Instead of reinstalling chrome you could just right click on favicon.ico and click on Block request URL to stop the browser from sending requests for favicon.ico您无需重新安装 chrome,只需右键单击 favicon.ico 并单击阻止请求 URL 以阻止浏览器发送对 favicon.ico 的请求

Problem solved: I reinstalled Chrome and it works fine now.问题已解决:我重新安装了 Chrome,现在工作正常。 Thanks all!谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM