运行服务器和使用浏览器浏览时,将调用ExpressJS路线两次
时,ExpressJS路线被调用两次
运行OS系统环境
const url = require('url');
const path = require("path");
const express = require('express');
let app = express();
console.log("instantiation");
app.use("/language", function (req, res) {
const queryObject = url.parse(req.url, true).query;
console.log("/ language");
res.send("Testing server : language");
});
app.use("/", function (req, res) {
console.log("/ path");
res.send("Testing server : main page");
});
app.use("*", function (req, res) {
console.log("* path");
res.send(`"Testing my server"`);
});
app.listen(3001, "127.0.0.1", function () {
console.log(`Server listening at ` + 3001);
});
:Windows 11,ExpressJS(“ Express”:“^4.18.1”,“版本”:“ 4.18.1”),Node V16.15.0更新(以下/
路由仍然被调用两次)。我缺少什么?:
const url = require('url');
const path = require("path");
const express = require('express');
let app = express();
console.log("instantiation");
app.use("/language", function (req, res) {
const queryObject = url.parse(req.url, true).query;
console.log("/ language");
res.send("Testing server : language");
});
app.use("/", function (req, res) {
console.log("/ path");
res.send("Testing server : main page");
});
app.listen(3001, "127.0.0.1", function () {
console.log(`Server listening at ` + 3001);
});
是我的系统还是Express/Node的真正问题?
Expressjs Routes are invoked twice when running
OS System Environment: Windows 11, ExpressJS ("express": "^4.18.1", "version": "4.18.1"), Node v16.15.0
const url = require('url');
const path = require("path");
const express = require('express');
let app = express();
console.log("instantiation");
app.use("/language", function (req, res) {
const queryObject = url.parse(req.url, true).query;
console.log("/ language");
res.send("Testing server : language");
});
app.use("/", function (req, res) {
console.log("/ path");
res.send("Testing server : main page");
});
app.use("*", function (req, res) {
console.log("* path");
res.send(`"Testing my server"`);
});
app.listen(3001, "127.0.0.1", function () {
console.log(`Server listening at ` + 3001);
});
Update (The following /
route is still invoked twice). Anything I am missing?:
const url = require('url');
const path = require("path");
const express = require('express');
let app = express();
console.log("instantiation");
app.use("/language", function (req, res) {
const queryObject = url.parse(req.url, true).query;
console.log("/ language");
res.send("Testing server : language");
});
app.use("/", function (req, res) {
console.log("/ path");
res.send("Testing server : main page");
});
app.listen(3001, "127.0.0.1", function () {
console.log(`Server listening at ` + 3001);
});
Is it my system or a real problem with express/node?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多次调用路由处理程序的原因是您使用
app.use('/')
而不是app.get('/')或
app.post('/')
。app.use()
是为了添加中间件,并且将匹配所请求的路径的 base (而不是使用方法处理程序,这将匹配 完整路径)。换句话说,
app.use('/')
将匹配在其之前声明的其他处理程序尚未处理的任何请求。这包括/favicon.ico
的请求,浏览器通常在打开页面时会执行。例如,它还将将请求与/foo/bar
相匹配。有关
app.use()
的更多信息,可以找到在这里。The reason that your route handler is invoked multiple times is because you're using
app.use('/')
instead of specific method handlers likeapp.get('/')
orapp.post('/')
.app.use()
is meant for adding middleware, and will match the base of a requested path (as opposed to using the method handlers, which will match the full path).In other words
app.use('/')
will match any request that isn't already handled by other handlers declared before it. This includes requests to/favicon.ico
, which a browser usually performs when opening a page. It will also match requests to/foo/bar
, for instance.More info on
app.use()
can be found here.