【问题标题】:Why does Chrome send two http requests while Firefox sends one? [duplicate]为什么 Chrome 发送两个 http 请求而 Firefox 发送一个? [复制]
【发布时间】:2020-05-13 03:20:22
【问题描述】:

编辑:所以第二个响应肯定是 favicon.ico 问题,但为什么这发生在 chrome 而不是 firefox 中?

原文: 我正在学习 node.js 的 express 并练习中间件的路由。我的问题是“/”路由正在通过,即使我没有发送请求并且我没有调用 next();在它之前的中间件中。此外,当我向“/”路由发送请求时,它会发送双重响应。它是在 chrome 而不是 firefox 中执行此操作的,所以有人可以解释一下区别吗?

const express = require('express');

const app = express();

app.use('/users', (req, res, next) => {
    console.log('This handles /users route');
    res.send('<h1>This handles /users route</h1>');
});

app.use('/', (req, res, next) => {
    console.log('This handles / route');
    res.send('<h1>This handles / route</h1>');
});

app.listen(3000);

如果我导航到 localhost:3000/users 页面显示正确的 html 但控制台日志:

  • 这处理 /users 路由
  • 这处理/路由

如果我导航到 localhost:3000/ 页面显示 html 正确但控制台日志:

  • 这处理/路由
  • 这处理/路由

【问题讨论】:

  • 只需在中间件中执行console.log(req.url) 即可查看发生了什么。您可能会看到 Firefox 和 Chrome 之间的 favicon.ico 缓存差异。但是,如果您添加我提到的日志,它肯定会告诉您。请记住,app.use('/', ...) 匹配向您的服务器发出的每一个请求,因此它总是会被命中,除非在它获取请求之前有什么东西。

标签: javascript node.js express http favicon


【解决方案1】:

默认情况下,Chrome 会请求网站 favicon.ico。像这样:

http://localhost:3000/favicon.ico

【讨论】:

  • 请务必说明您发布的链接指向的位置。 "enter image description here" 还不够^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 2022-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多