【发布时间】:2021-01-09 05:22:33
【问题描述】:
我刚开始学习NodeJS,请帮助我理解这一点
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>Hello From Express</h1>');
});
app.listen(3000);
输出是
In the Middleware
In another Middleware
In the Middleware
In another Middleware
预期
In the Middleware
In another Middleware
【问题讨论】:
-
您如何向您的服务器发出请求?从浏览器?卷曲命令?还有什么?
-
我在本地服务器上执行此操作:localhost:3000 上的 node app.js
-
@ChetanBatra 我尝试运行它,但没有发现输出有任何问题。
-
是的,但是直到你真正向服务器发出请求(例如,在浏览器中打开
http://localhost:3000),中间件才会执行。你是如何提出这个要求的? -
打开开发工具并查看网络选项卡。然后刷新页面。浏览器向您的 URL 发出多少请求?有时浏览器会发出多个请求(例如 CORS 预检请求、获取 favicon.ico 文件的请求等)。另外,尝试运行
curl localhost:3000并查看输出是什么。在这种情况下,您会看到两次文本输出吗?
标签: javascript node.js express mern