Application diagnostics
Inspect an application's mount location during setup or diagnostics.
Express.js · Module
app.path() returns the canonical mount path of an Express application as a string. For complex mounted-app setups, prefer req.baseUrl during request handling.
Call app.path() to obtain the application's canonical mount path as a string. For mounted applications, the result can become difficult to interpret; the documentation recommends req.baseUrl when handling a request and needing the canonical path.
app.path()String
const express = require('express');
const app = express();
const admin = express.Router();
admin.get('/users', (req, res) => res.send('users'));
app.use('/admin', admin);
console.log(app.path()); // ""
console.log(admin.path()); // "/admin"
Creates an Express application and a mounted router, then prints each object's canonical path.
Inspect an application's mount location during setup or diagnostics.
Compare the paths of an app and mounted sub-application.
Log an application's canonical path outside an individual request.
app.path() returns the canonical path of the Express application as a string. For an application that is not mounted beneath another path, this is commonly the empty string.
Follow-up: What should code use inside a request when mounted-app behavior is complex? Use req.baseUrl, because the documentation recommends it for obtaining the canonical application path.
The method reports the path at which the application is mounted. It does not return a URL and does not inspect an individual request.
Follow-up: How can middleware discover its current mount prefix? Read req.baseUrl during request processing rather than depending on app.path() in complex mount trees.
Mounted applications can make app.path() behavior complicated. The documented alternative is req.baseUrl, which is intended to provide the canonical path in request handling.
Follow-up: Why is req.baseUrl preferable there? Its value is derived from the current request's mount context, so it reflects the path actually traversed by that request.
req.baseUrlexpress()express.Router()app.use()