【问题标题】:How can i get the name of this JSON or get the path from NodeJS Express Router?如何获取此 JSON 的名称或从 NodeJS Express 路由器获取路径?
【发布时间】:2020-07-24 14:57:32
【问题描述】:

我正在尝试将命名路由器功能添加到 NodeJS 的快速路由器(这是一种做法,我不想下载任何其他包),我已经成功了,但只剩下一件事.. . 路径,我的意思是,如果我有这样的路线:

index.js

const express = require('express');
const app = express();
app.use(require('./routes'));

routes/index.js

const express = require('express');
const expressRouter = express.Router();
const router = require("./namedRoutes")(expressRouter); // <- This is my own named router
const ViewsController  = require('../app/controllers/ViewsController');

router.get("/", ViewsController.loadHome).setName("home");
router.get("/fromHome", ViewsController.loadHome).setName("fromHome");

module.exports = router;

我可以使用以下方法从我的助手(车把)获取路线名称:

{{ route "home" }} 或 {{ route "fromHome" }}

它还给我:

localhost:3000/ 或 localhost:3000/fromHome

但是当我尝试在 mi index.js

上使用此语法添加路径时

app.use("/other/path", require('./routes'));

我的命名路线将再次返回localhost:3000/ 或localhost:3000/fromHome,但没有路径。

在我的路由助手中进行测试,我看到我可以从请求对象中获取所有注册的路由,这是仅注册一个对象路由的示例(这些路由对象位于req.app._router.stack 中的数组内部) :

Layer {
    handle: [Function: router] {
      params: {},
      _params: [],
      caseSensitive: undefined,
      mergeParams: undefined,
      strict: undefined,
      stack: [Array],
      _get: [Function],
      _post: [Function],
      _put: [Function],
      _delete: [Function],
      get: [Function],
      post: [Function],
      put: [Function],
      delete: [Function],
      setName: [Function],
      lastRouteAdded: '/fromOthers',
      names: [Array]
    },
    name: 'router',
    params: undefined,
    path: undefined,
    keys: [],
    regexp: /^\/other\/path\/?(?=\/|$)/i { fast_star: false, fast_slash: false },
    route: undefined
  }

如您所见,path 未定义,但如果您看到,有一个名为:

regexp: /^\/other\/path\/?(?=\/|$)/i { fast_star: false, fast_slash: false }

这是一个包含路径的正则表达式,所以如果我能得到那个正则表达式,我就能得到路径,但如果我尝试这样做,似乎没有办法得到它(假设 var @ 987654334@ 包含此 JSON 对象):

const path = routeInstance.regexp;
console.log(path);

我明白了:

/^\/other\/path\/?(?=\/|$)/i { fast_star: false, fast_slash: false }

但是现在我不能得到那个正则表达式,如果我这样做:

console.log(Object.keys(path));

我明白了:

[ 'fast_star', 'fast_slash' ]

如您所见,现在没有正则表达式,如果我这样做:

console.log(Object.keys(routeInstance));

(记住 var routeInstance 包含 Layer 对象)我明白了:

[
  'handle', 'name',
  'params', 'path',
  'keys',   'regexp',
  'route'
]

那么,我怎样才能得到那个正则表达式?或者如果有其他方法可以得到它,我怎么能得到它?

注意:

我知道app.use("/other/path", require('./routes')); 允许您放置一个正则表达式或数组而不是静态路径字符串,所以我的想法是route 帮助器根据过去的正则表达式返回最明显的路线。

感谢您的回复!!!

【问题讨论】:

    标签: javascript node.js json express routes


    【解决方案1】:

    好的,我已经解决了,我可以让那个正则表达式这样做:

    const regexpPath = routerInstance.regexp.toString();
    

    似乎 JSON 对象被丢弃,只返回正则表达式字符串。

    所以现在我可以用这个得到路径:

    // Search all ocurrences of type /some (Returns an array)
    const pathOcurrences = regexpPath.match(/\/[A-Za-z0-9]+/igm);
    
    // Remove the last element 'cause it will still bring me the / i of the regexp
    pathOcurrences.pop();
    
    // Join the ocurrences
    const path =  pathOcurrences.join("");
    

    显然,对于不是路径的正则表达式,它会失败,但这是正确的,你试图从正则表达式中获取路径,你不能让它工作^-^

    【讨论】:

      猜你喜欢
      • 2017-02-15
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多