HenningM / express-ws

WebSocket endpoints for express applications
BSD 2-Clause "Simplified" License
877 stars 142 forks source link

Express Request params empty when using nested Router #124

Closed tobloef closed 5 years ago

tobloef commented 5 years ago

Hey there and thanks for creating this module, it's pretty neat.

There seems to be a bug with the way the req.params field is handled though. I have a couple of routers setup up something like this:

const specificThingRouter = Router();
specificThingRouter.ws("/ws", (ws, req) => console.log(req.params.thingId);

const thingsRouter = Router();
thingRouter.use("/:thingId", specificThingRouter);

app.use("/things", thingsRouter);

But when I hit the endpoint, the thingId parameter is just undefined. In fact, the entire params object is empty.

If I use some middleware to print the thingId as it goes through the routers, I can see that the thingId paramater is indeed defined when it's still just in an Express context. If I just create a ws endpoint directly on the app, any parameters work find. This leads me to believe that there's an issue with how the params are handed over to the req in the callback from the ws function this library provides, when using Routers.

Let me know if there's any more info I should add. I don't have a lot of time right now, but I might be able to make a pull request later, if the maintainer(s) of this repo don't have the time either.

Thanks!

tobloef commented 5 years ago

My bad, this actually seems to be related to Express itself, not express-ws. More info here: https://gist.github.com/zcaceres/f38b208a492e4dcd45f487638eff716c