Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Module

app.path

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.

How it works

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.

Syntax

app.path()

Returns

String

Key points

  • app.path() returns the application's canonical path as a string.
  • The method is called on an Express application object, conventionally named app.
  • Within request processing, req.baseUrl is generally the better way to obtain the canonical application path.
  • Nested or otherwise complex mounting can make the method's result difficult to reason about.

Common mistakes

  • Avoid relying on app.path() for complicated mounting arrangements; use req.baseUrl when a request context is available.

Example: Read an app path

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.

When to use it

Application diagnostics

Inspect an application's mount location during setup or diagnostics.

Mounted application inspection

Compare the paths of an app and mounted sub-application.

Startup logging

Log an application's canonical path outside an individual request.

Interview questions

QWhat does app.path() return?

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.

QWhat does the returned path represent?

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.

QWhen should app.path() be avoided?

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.baseUrl
  • express()
  • express.Router()
  • app.use()

Source: Application Object · Express.js 4.x. Checked 2026-10-15.