Open In App

Express.js | app.path() Function

Last Updated : 16 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The app.path() function returns the canonical path of the app as a string. When mounting advanced apps, the behavior of this approach can become extremely complicated. In most cases, it is better to use req.baseUrl to obtain the app’s canonical path.

Installation of the express module:

  1. You can visit the link to Install the express module. You can install this package by using this command.
npm install express
  1. After installing the express module, you can check your express version in the command prompt using the command.
npm version express
  1. After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.
node index.js

Filename: index.js 

javascript




const express = require('express');
const app = express();
const PORT = 3000;
 
const app = express()
const blog = express()
const blogAdmin = express()
 
app.use('/user', blog)
blog.use('/admin', blogAdmin)
 
console.dir(app.path()) // ''
console.dir(blog.path()) // '/blog'
console.dir(blogAdmin.path()) // '/blog/admin'


Steps to run the program:

  1. The project structure will look like this:
  2. Make sure you have installed the express module using the following command:
npm install express
  1. Run the index.js file using the below command:
node index.js

Output:

''
'/user'      
'/user/admin'

So this is how you can use the express app.path() function that returns the canonical path of the app as a string.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads