javascript - 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 吗?
解决方案
你如何发送请求?这很可能是两个不同的请求,如果您通过打印网址,req.url
您将能够看到它。
如果您通过浏览器发送请求,则可能是 GET 请求和对站点图标的请求。
推荐阅读
- php - 如何使用单一形式在两个具有关系的表中添加值?
- python - 如何将 0 和 1 转换为二进制并返回用于 Huffman 算法?
- javascript - 如何给“复选框 onChange 事件”调用输入 ID(在 Xpages 中)
- c - Else 语句在 strcmp 中返回错误结果(比较哈希值)(更新)
- javascript - 为什么 jQuery-migrate 不直接在代码中替换折旧的函数?
- prolog - SWI Prolog CLP(FD) 调度
- tomcat - 如何在 EL Stream API 中使用 Collectors#joining?
- dask - dask 数据框的 read_csv 无法正确读取 csv
- linux - 将 rsa 密钥部署到其他机器
- ios - 用于注册的 gmail 集成