express routing and path params


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

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


By default express router does not inherit parent router params

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

should do the trick

Answered By – shantr

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published