首页 > 解决方案 > express js,为什么服务器有两个响应?

问题描述

我刚开始学习 express.js 和 node.js。我正在尝试创建一个简单的服务器,这是代码。

    const http = require('http');

    const express = require('express');

    const app = express();
    app.use((req, res, next)=>{
      console.log('in the middleware');
      next();
    });
    app.use((req, res, next)=>{
      console.log('in another middleware');
      res.send('<h1> I will handle this</h1>');
    });
    const server = http.createServer(app);

    server.listen(3000);

它正在工作,但在控制台中我看到了两次:在另一个中间件中的中间件中 在另一个中间件中的中间件中

我不应该只看到: in the middleware in another middleware 吗?

标签: javascriptnode.jsexpressvisual-studio-code

解决方案


你如何发送请求?这很可能是两个不同的请求,如果您通过打印网址,req.url您将能够看到它。

如果您通过浏览器发送请求,则可能是 GET 请求和对站点图标的请求。


推荐阅读