Reputation: 3309
I'm facing a very specific use case, where I need to access the mount paths of various express sub apps when iterating over the app object.
Sample code :
const express = require("express")
const app = express()
app.use("/users", new usersRouter())
app.use("/flowers", new flowersRouter())
// Later in code...
app.get("/something", (req, res, next) => checkAppObject(app, next))
function checkAppObject(app, next) {
// Where are stored the "/users" and "/flowers" info ?
console.log(app)
next()
}
I didn't find it anywhere
Upvotes: 0
Views: 298
Reputation: 3309
As it turns out, the express app
object contains an internal _router.stack
property which is populated by a collection of Layer
objects.
When doing app.use('/path', someFunc)
, express adds a Layer
object and if this someFun
is also a router (an app
object itself), then the layer.handle
object will also contain its own stack
property (and so on recursively).
Hence, the only way to access the mount point is on this layer object, inside a regexp
. One would have to serialize a regex or something (if that's even possible, see this)
Provided the mount path is "/batchs"
the value on the layer object is :
{ /^\/batchs\/?(?=\/|$)/i fast_star: false, fast_slash: false }
on my computer.
Hope this helps
Upvotes: 1