Express.js App Path Method



The app.path() method returns the canonical path. The path is returned as a string. It is better to use the req.baseUrl method since the app.path() method can be very complicated in complex cases of mounted apps.

Syntax

app.path( )

Example 1

Create a file with the name "appPath.js" and copy the following code snippet. After creating the file, use the command "node appPath.js" to run this code.

// app.path() Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); var PORT = 3000; // Assigning express constructor var app = express() var blog = express() var blogAdmin = express() // Assigning the below url's app.use('/api', blog) blog.use('/v1', blogAdmin) // printing values as per the URLs console.dir(app.path()) console.dir(blog.path()) console.dir(blogAdmin.path())

Output

C:\home
ode>> node appPath.js '' '/api' '/api/v1'

Example 2

Let’s take a look at one more example

// express.raw() Demo Example // Importing the express module var express = require('express'); // Initializing the express and port number var app = express(); var PORT = 3000; // Assigning express constructor var app = express() var blog = express() // Assigning the below url's app.use('/api', blog) app.use('/v1', blog) // printing values as per the URLs console.dir(app.path()) // '' console.dir(blog.path()) // '/blog'


Output

C:\home
ode>> node appPath.js '' '/v1'
Updated on: 2021-09-30T13:55:55+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements