如何知道路由器上的挂载路径?

Ant*_*eda 7 router node.js express

我在网络应用程序上使用快速' 路由器.我用通常的方式添加路由器:

app.use(router, '/myroutehere/');
Run Code Online (Sandbox Code Playgroud)

每个路由器的处理程序都不知道它们被"安装"的位置(不同的文件,不同的关注点等).直到现在我还需要为其中一个路由器创建一个ToC(并且内容是动态生成的).我正在使用url.resolve处理相对路径,但我错过了url的初始部分(否则链接将解析/而不是/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
});
Run Code Online (Sandbox Code Playgroud)

std*_*b-- 6

那么"router.mountpath"怎么样?

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

UPD.好的,你需要:<req.route.path>, <req.baseUrl>, <req.originalUrl>

  • 好的,你需要:`&lt;req.route.path&gt;, &lt;req.baseUrl&gt;, &lt;req.originalUrl&gt;` (2认同)