简体   繁体   中英

Pass a route segment as callback argument

I'm building my first app in express. Is it possible to somehow pass a route segment as an argument to a callback?

app.get('/connect/:mySegment', myCallback(mySegment));

Specifically, I'm using passport with several strategies for authentication. So rather than doing,

app.get('/connect/twitter',
  passport.authorize('twitter')
);
app.get('/connect/facebook',
  passport.authorize('facebook')
);

I would like to do something along the lines of...

app.get('/connect/:service', passport.authorize(service));

Of course, you can do

app.get('/connect/:mySegment', function(req, res){
    // then you can use req.params.mySegment
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM