Home > Software design >  express routing and path params
express routing and path params

Time:09-01

I have the following routing in app.js

app.use("/api/:phone/stuff", userRoutes);

In userRoutes, I have the following

userRoutes.get("/", userController.getStuff);

The client (Android app, retrofit) requests the following url: GET /api/ 155555555/stuff, in userService I have getStuff method which is executed as expected, meaning it routes it well.

in a validation middleware, I see that req.originalUrl contains the url as expected but when viewing req.params, it is an empty object. I was expecting it to link the phone part of the url to :phone param so I will have req.params.phone.

I thought the problem was the sign but it is urlencoded and even without it, it didn't work. What am I missing?

CodePudding user response:

By default express router does not inherit parent router params

const userRoutes = express.Router({ mergeParams: true });

should do the trick

http://expressjs.com/en/4x/api.html#express.router

  • Related