如何知道路由器在快车上的安装路径?

问题描述 投票:7回答:1

我正在Web应用程序上使用Express'Router。我以通常的方式添加路由器:

app.use(router, '/myroutehere/');

每个路由器的处理程序都不知道它们被“挂载”在哪里(不同的文件,不同的关注点等)。直到现在,我需要为其中一个路由器创建一个ToC(并且内容是动态生成的),这种方法行之有效。我正在使用url.resolve处理相对路径,但缺少网址的开头部分(否则链接将解析为/而不是/myrouter/)。

有没有一种方法可以知道路由器的安装路径,而无需在不同的地方对其进行硬编码?

router.get('/', function(req, res){
  // Need to know the mount path here or a way to resolve relative links
  // to the mount path and make them absolute to the app
});
node.js express router
1个回答
6
投票

“ router.mountpath”呢?

app.mountpath属性包含一个或多个路径模式,子应用已安装

http://expressjs.com/en/api.html#app.mountpath

更新好的,您需要:<req.route.path>, <req.baseUrl>, <req.originalUrl>

© www.soinside.com 2019 - 2024. All rights reserved.