How to find out the way to mount the router on express?

I use the expression " Router in a web application. I add routers in the usual way:

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

The handlers for each router have no idea where they were "installed" (different files, different problems, etc.). So far, this worked fine, that I need to create a ToC for one of the routers (and the content is dynamically generated). I use url.resolve to handle relative paths, but I miss the initial part of the url (otherwise the links will be resolved to / instead of /myrouter/ ).

Is there a way to find out how to mount a router without hard coding in different places?

 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 }); 
+6
source share
1 answer

What about "router.mountpath"?

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

Update. Ok, you need: <req.route.path>, <req.baseUrl>, <req.originalUrl>

+5
source

Source: https://habr.com/ru/post/989031/


All Articles