Express.js app.path() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Application

Definition

app.path() returns the canonical path of an Express app as a string — how it sits in a parent mount chain. The top-level app returns ''; a sub-app mounted at /blog returns '/blog'.

You mount sub-apps with app.use(). Related mount metadata lives on app.mountpath; for request-time prefixes prefer req.baseUrl.

Syntax

Call app.path in your Express.js app. Start with this line.

Express
app.path()
CallWhat it does
app.path()Primary signature.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

app.path()

Parameters

ParameterDescription
StringRequired. Canonical mount path for this app. Root: ''. Nested example: '/blog/admin'.

Return Value

app.path() takes no arguments and returns a String: the canonical mount path built from parent mounts. The root app returns an empty string (''), not '/'.

Express
app.path(); // '' on the top-level app

const admin = express();
app.use('/admin', admin);
admin.path(); // '/admin'

Express.js Examples

Example 1 — Nested Mounts

Official-style tree: root → blog → blogAdmin.

Express
const express = require('express');

const app = express();
const blog = express();
const blogAdmin = express();

app.use('/blog', blog);
blog.use('/admin', blogAdmin);

console.log(app.path());       // ''
console.log(blog.path());      // '/blog'
console.log(blogAdmin.path()); // '/blog/admin'

app.listen(3000);

How It Works

  1. Three apps. Each is a full Express application.
  2. Mount chain. Parent mounts blog; blog mounts admin.
  3. Root path. app.path() is '' — nothing above it.
  4. Joined path. Deeper apps concatenate mount segments into one string.

Example 2 — path() vs mountpath

Same tree, different questions: immediate mount pattern vs full canonical path.

Express
const express = require('express');

const app = express();
const blog = express();
const blogAdmin = express();

app.use('/blog', blog);
blog.use('/admin', blogAdmin);

console.log('blog.mountpath =', blog.mountpath);           // '/blog'
console.log('blog.path() =', blog.path());                 // '/blog'
console.log('blogAdmin.mountpath =', blogAdmin.mountpath); // '/admin'
console.log('blogAdmin.path() =', blogAdmin.path());       // '/blog/admin'

app.listen(3000);

How It Works

  1. mountpath. Only the segment from the immediate parent (/admin for blogAdmin).
  2. path(). Walks parents and joins segments (/blog/admin).
  3. Shallow apps. For a one-level mount, both often look the same.
  4. Nested apps. Differences show up once mounts are stacked.

Example 3 — Prefer req.baseUrl

Inside a request, use the request’s matched prefix — not path() — for links.

Express
const express = require('express');

const app = express();
const blog = express();

app.use('/blog', blog);

blog.get('/posts', (req, res) => {
  res.json({
    pathMethod: blog.path(),   // '/blog' — mount structure
    baseUrl: req.baseUrl,      // '/blog' — this request’s prefix
    tip: 'Prefer req.baseUrl in handlers'
  });
});

app.listen(3000);

How It Works

  1. Mount. Blog lives under /blog.
  2. path(). Reports the app’s place in the tree (startup-friendly).
  3. baseUrl. Comes from the current match — safer for redirects and links.
  4. Docs tip. When mounts get complex, lean on req.baseUrl.

Runtime Support

app.path() is an Express.js application method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only hit the mounted URLs.

✓ Server · Express 4 & 5

app.path()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
app.path() Express on Node.js

Bottom line: Run examples with Node.js and the express package, then log app.path() after mounts.

Frequently Asked Questions

A string: the canonical path of that app in the mount hierarchy. The root app returns an empty string ''.
mountpath is the pattern used when this app was mounted by its parent (string or array). path() walks parents and joins those mounts into one canonical string.
Usually prefer req.baseUrl. Official docs note path() can get complicated with complex mounts; baseUrl is the matched URL prefix for the current request.
No. It reflects how apps are mounted, not the current request URL.
No. It is a server-side Express application method on Node.js.

Next: app.post()

Handle HTTP POST requests with Express route handlers.

app.post() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful