如何知道路由器在express上的挂载路径?
How to know the mount path of router on express?
我在 Web 应用程序上使用 express' Router。
我以通常的方式添加路由器:
app.use(router, '/myroutehere/');
每个路由器的处理程序都不知道他们去过哪里"mounted"(不同的文件、不同的关注点等)。直到现在我需要为其中一个路由器创建一个 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
});
"router.mountpath"呢?
The app.mountpath property contains one or more path patterns on which
a sub-app was mounted
http://expressjs.com/en/api.html#app.mountpath
Upd. 好的,你需要:<req.route.path>, <req.baseUrl>, <req.originalUrl>
我在 Web 应用程序上使用 express' Router。 我以通常的方式添加路由器:
app.use(router, '/myroutehere/');
每个路由器的处理程序都不知道他们去过哪里"mounted"(不同的文件、不同的关注点等)。直到现在我需要为其中一个路由器创建一个 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
});
"router.mountpath"呢?
The app.mountpath property contains one or more path patterns on which a sub-app was mounted
http://expressjs.com/en/api.html#app.mountpath
Upd. 好的,你需要:<req.route.path>, <req.baseUrl>, <req.originalUrl>