首页 > 解决方案 > 为什么 Chrome 发送两个 http 请求而 Firefox 发送一个?

问题描述

编辑:所以第二个响应肯定是 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 但控制台日志:

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

标签: javascriptnode.jsexpresshttpfavicon

解决方案


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

http://localhost:3000/favicon.ico

要求

已收到


推荐阅读