Shikha thakur Shikha thakur - 1 month ago 11
Node.js Question

understanding app.mountpath property of express

I am new to express framework and trying to learn basics but i don't understand app.mountpath property of express js .

i have gone through the docs but still very confused.

Any explaination is appreciated


Any express application (like var app = express()) has its own Router. You can use the app.use() function to mount routers into each other e.g.:

var app = express();
var router = express.Router();

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

This mounts router at the /route route pattern of the app's Router. The mountpath property simply contains the route pattern where a specific sub-app was mounted i.e. in the above example:

console.log(router.mountpath); // /route

Update: Maybe an additional remark about the point of this property. The docs show that mounting a sub-app abstracts the full path from the sub-app e.g.:

router.get('/sub', function(req, res) { ... });

This route is actually reached by /route/sub and not by /sub as we mounted it at /route. So if you want to access the path where a mounted sub-app is actually located you can use the app.mountpath property.