2

I need to have two routes, they are similar in the start but different in ends, how can I enforce the exact pattern for the respective route to be called.

Edit - Actually the problem lies in the app.use statement, I've not reached the get route yet.

A)

app.get("/admin/event/:id/sessions", function(request, response){

B)

app.get("/admin/event/:id", function(request, response){

C)

app.use("/admin/event/:id", function(request, response, next){

D)

app.use("/admin/event/:id/sessions", function(request, response, next){

C is getting called for the request GET /admin/event/1/sessions, my expectation is that D should be getting called. Please advise.

user2727195
  • 7,122
  • 17
  • 70
  • 118
  • You've noted your expectations, but haven't explained what's happening instead? The route under `A` should match the `GET` path you mentioned. Is it not? – Jonathan Lonowski Jun 01 '15 at 03:40
  • yes for some reason, the B is getting called for `GET /admin/event/1/sessions`, I've app.use in place for both of calls as well, i hope it shouldn't conflict – user2727195 Jun 01 '15 at 03:41
  • [`app.use()` is intended for adding middleware](http://stackoverflow.com/questions/15601703/difference-between-app-use-and-app-get-in-express-js), not for routes. Its path is instead a *prefix* with an assumed wildcard at the end and applies to all methods (`GET`, `POST`, etc.). – Jonathan Lonowski Jun 01 '15 at 03:50
  • Can I restrict `app.use` to stay within it's jurisdiction only – user2727195 Jun 01 '15 at 03:51
  • "Every request starting with a partial path" is its jurisdiction. – Jonathan Lonowski Jun 01 '15 at 03:52
  • Any lateral thinking? I need to have a different middle ware for subpaths, – user2727195 Jun 01 '15 at 03:53
  • You can provide middleware to the [routing methods](http://expressjs.com/4x/api.html#app.METHOD) to have them apply to specific routes -- `app.get('/path', someMiddleware(), function (req, res) { ... });`. – Jonathan Lonowski Jun 01 '15 at 03:57
  • sounds promising, I'm going to try this one :) – user2727195 Jun 01 '15 at 04:13
  • thanks, works like a charm – user2727195 Jun 01 '15 at 04:24
  • need another trick... :) is there a way to have multiple functions getting executed for the same route, `app.get("/param1"..` and another place, `app.get("/param1`, reason to use different functions for content negotiation. https://en.wikipedia.org/wiki/Content_negotiation. Reason: They are different modules for the same resource type but different contents – user2727195 Jun 01 '15 at 07:24

0 Answers0