How to obtain route path?

468 views
Skip to first unread message

algebris

unread,
Mar 16, 2016, 6:30:07 PM3/16/16
to Express
Hi,

In this piece of code it works well for me ...

app.get('/user/:id', function(req,res,next){
     console.log(req.route.path); // It works well
});

But I would like obtain route path in this way

app.use(function(req, res, next){
    console.log(req.route.path); // It doesn't work!!
});

In order to get swagger documentation node by key "/user/:id" for all my routes.

It would be extremely useful if I could intercept that path not only inside
app.METHOD()
but in general for all requests

How can I do that in this way???

Thank you for your support folks!

Protosac Sac

unread,
Apr 18, 2016, 8:13:36 AM4/18/16
to Express
You're missing the route as the first argument:

app.use('/', function(req, res, next) {

Protosac Sac

unread,
Apr 18, 2016, 8:13:36 AM4/18/16
to Express
You're missing the route as the first argument:

app.use('/',function(req, res, next) {

   console
.log(req.path) // you can also add req.url, req.method, etc
   
next();
}
 



On Wednesday, March 16, 2016 at 3:30:07 PM UTC-7, algebris wrote:

Protosac Sac

unread,
Apr 18, 2016, 8:13:37 AM4/18/16
to Express

You're missing the route as the first argument.
    
app.use('/',function(req, res, next){

   console
.log(req.path); // though you can log req.method, req.url also
}
 


On Wednesday, March 16, 2016 at 3:30:07 PM UTC-7, algebris wrote:

Andrew Liu

unread,
Apr 26, 2016, 1:54:48 PM4/26/16
to Express
You can use 'app.all()' instead of 'app.use()'.

在 2016年3月17日星期四 UTC+8上午6:30:07,algebris写道:

Andrew Liu

unread,
Apr 26, 2016, 1:54:48 PM4/26/16
to Express
It seems that 'req.route' is specified in function 'Route.prototype.dispatch'
And 'req.route' is undefined unless 'this._router.route' is invoked.

you can find these functions in Express source code.
'express/lib/application.js'
'express/lib/router/index.js'
'express/lib/router/route.js'


在 2016年3月17日星期四 UTC+8上午6:30:07,algebris写道:
Hi,
Reply all
Reply to author
Forward
0 new messages