【问题标题】:Node.js Express - Why next("route") does not lead to the next route?Node.js Express - 为什么 next("route") 不会导致下一条路线?
【发布时间】:2019-09-26 03:32:29
【问题描述】:

这是我的代码,它是用 Node express 构建的。 next("route") 在 2 种类似情况下无法统一工作。

let express = require("express");
let app = express();
let router = express.Router();

router.use("/message/:id", function (req, res, next) {
    console.log(typeof req.params.id);
    if (req.params.id === 1 + "") {
        console.log("the current id is 1");
        next();
    } else if (req.params.id === 2 + "") {
        console.log("the current id is 2");
        next("route");
    } else if (req.params.id === 3 + "") {
        console.log("the current id is 3");
        next("router");
    } else {
        res.send("no id matched");
    }
}, function (req, res) {
    res.send("this is a scenario when id is 1");
});

router.use("/message/:id", function (req, res) {
    res.send("this is a details page");
});


app.use("/", router, function (req, res) {
    res.send("this is triggered by the app");
});

app.listen(8080, function () {
    console.log("Please visit localhost:8080");
});

当我输入如下URL地址:http://localhost:8080/message/2,Node REPL控制台输出:“当前id为2”,网页显示: "这是id为1时的场景".

我对此感到很困惑。根据 express 的官方文档,next("route") 会将控制权交给下一个路由。

所以我假设它应该显示“这是一个详细信息页面”,而不是“这是 id 为 1 时的场景”。为什么?


为了了解更多信息,我还对代码进行了一些更改:

let express = require("express");
let app = express();
let router = express.Router();

router.get("/message/:id", function (req, res, next) {
    console.log(typeof req.params.id);
    if (req.params.id === 1 + "") {
        console.log("the current id is 1");
        next();
    } else if (req.params.id === 2 + "") {
        console.log("the current id is 2");
        next("route");
    } else if (req.params.id === 3 + "") {
        console.log("the current id is 3");
        next("router");
    } else {
        res.send("no id matched");
    }
}, function (req, res) {
    res.send("this is a scenario when id is 1");
});

router.get("/message/:id", function (req, res) {
    res.send("this is a details page");
});


app.use("/", router, function (req, res) {
    res.send("this is triggered by the app");
});

app.listen(8080, function () {
    console.log("Please visit localhost:8080");
});

我只是简单地将 router.use 替换为 router.get,这次当我重新访问 "http://localhost:8080/message/2" 时,网页显示“这是一个详细信息页面”

为什么在这两种情况下结果不同?

【问题讨论】:

  • 因为你打电话给next('router'),而不是next('route')
  • 你能澄清更多吗?
  • 我在 express 文档中找不到对 next("router") 的引用,你介意链接到你读到这是一个有效选项的地方吗?
  • 搜索这行文本“要跳过路由器中间件堆栈中的其余中间件功能,请调用 next('route') 将控制权传递给下一个路由。注意:next('route' ) 仅适用于使用 app.METHOD() 或 router.METHOD() 函数加载的中间件函数。”在“expressjs.com/en/guide/using-middleware.html
  • 在“expressjs.com/en/guide/using-middleware.html”搜索下一个('router')

标签: javascript node.js express


【解决方案1】:

据说在docs那个

要跳过路由器中间件堆栈中的其余中间件函数,请调用 next('route') 将控制权传递给下一个路由。注意:next('route') 仅适用于使用 app.METHOD() 或 router.METHOD() 函数加载的中间件函数。

注意 NOTE 部分,next('route') 意味着跳过,但在第一种情况下它没有跳过,因为你没有使用 route.METHOD(),你使用了 router.use()

【讨论】:

【解决方案2】:

router.get 仅用于定义子路径

var router = express.Router();
app.use('/api', router); // Mount the router as middleware at path /api

router.get('/signup', signup);
router.get('/user', userInfo);

If you open /api/signup, then the signup function will get called.
If you open /api/user, then the userInfo function will get called.

在使用 router.use() 时,您只能提供中间件,如下所示:

router.use(function(req, res, next) {
  console.log('%s %s %s', req.method, req.url, req.path);
  next();
});

【讨论】:

  • 谢谢我已经找到答案了!
猜你喜欢
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
相关资源
最近更新 更多