gpt4 book ai didi

javascript - 嵌套路由器中的 ExpressJS req.params 为空

转载 作者:数据小太阳 更新时间:2023-10-29 06:11:16 25 4
gpt4 key购买 nike

我使用的路由设置使用我的“api”文件夹的目录结构来直观地设置路由。但是,在我使用文件夹名称表示路由参数的情况下,req.params 在 Controller 中未定义。

有问题的路由是:

GET /api/google/accounts/:account_id/analytics/profiles/

这是我的路线加载器。它基本上在 api 文件夹上执行 glob 查找名为 routes.js 的文件,并在适当的路径上执行 app.use(由文件夹结构确定)。

  // load routers
files = glob.sync("api/**/routes.js");

console.log(files);
// [ 'api/campaigns/routes.js',
// 'api/google/accounts/:account_id/analytics/profiles/routes.js',
// 'api/google/accounts/routes.js',
// 'api/google/urls/routes.js',
// 'api/users/routes.js' ]

// use each router on the appropriate route
for(var i=0;i<files.length;i++) {

route = "/" + files[i].split("/routes.js")[0];

console.log(route);
// '/api/google/accounts/:account_id/analytics/profiles'

router = require(path.join(__dirname, files[i]))(config);
routes = router.stack;
app.use(route, router);

// list all registered routes for this router
for(var j=0;j<routes.length;j++) {
routeDebug((" " + Object.keys(routes[j].route.methods)[0].toUpperCase()).slice(-6) + " " + route + routes[j].route.path);
}
}

“api”文件夹结构是:

./api
./api/campaigns
./api/campaigns/controller.js
./api/campaigns/model.js
./api/campaigns/routes.js
./api/users
./api/users/controller.js
./api/users/model.js
./api/users/routes.js
./api/google
./api/google/accounts
./api/google/accounts/:account_id
./api/google/accounts/:account_id/analytics
./api/google/accounts/:account_id/analytics/profiles
./api/google/accounts/:account_id/analytics/profiles/controller.js
./api/google/accounts/:account_id/analytics/profiles/routes.js
./api/google/accounts/controller.js
./api/google/accounts/model.js
./api/google/accounts/routes.js
./api/google/urls
./api/google/urls/controller.js
./api/google/urls/routes.js

在上面的循环中,routeDebug 是调试 npm 包的包装器。输出是:

  routes    GET /api/campaigns/ +0ms
routes POST /api/campaigns/ +2ms
routes GET /api/campaigns/:campaign_id +1ms
routes PUT /api/campaigns/:campaign_id +0ms
routes DELETE /api/campaigns/:campaign_id +0ms
routes GET /api/google/accounts/:account_id/analytics/profiles/ +128ms
routes GET /api/google/accounts/ +4ms
routes POST /api/google/accounts/ +0ms
routes POST /api/google/urls/ +3ms
routes GET /api/users/ +12ms
routes POST /api/users/ +0ms
routes GET /api/users/:user_id +0ms
routes PUT /api/users/:user_id +0ms
routes DELETE /api/users/:user_id +0ms

因此我们可以看到所讨论的路由正在按预期设置。但是 req.params 是未定义的。其他路由没有这个问题。

为什么只有这条特定路线的 req.params 是空的?有没有比使用文件夹作为路由参数更好的方法来构建这些嵌套路由?

编辑:我预计问题源于嵌套路由器无法访问父路由器的参数。

Rest with Express.js nested router

但是,按照建议设置子路由器也无济于事。

var router = require("express").Router({mergeParams: true});
// no difference

最佳答案

原来是mergeParams was not added to Express until 4.5.0 ;我的项目还在 4.2.0 上。更新到 4.5.0 并将 {mergeParams: true} 传递给子路由器解决了这个问题。有关详细信息,请参阅此类似问题:

Rest with Express.js nested router

关于javascript - 嵌套路由器中的 ExpressJS req.params 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29102672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com