运行服务器和使用浏览器浏览时,将调用ExpressJS路线两次

发布于 2025-02-02 02:15:53 字数 1410 浏览 1 评论 0原文

时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆伤 2025-02-09 02:15:53

多次调用路由处理程序的原因是您使用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 like app.get('/') or app.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文