express-ws icon indicating copy to clipboard operation
express-ws copied to clipboard

res.locals should be passed to express-ws middlewares

Open steffansluis opened this issue 9 years ago • 2 comments

Express exposes a locals property on the response object that it meant to be used to pass data between middlewares for the current request only. I think this property should be passed on the ws object, probably here. I'd be happy to do so and open a PR if you agree! In any case, awesome library!

steffansluis avatar Jun 09 '16 09:06 steffansluis

Hi & thanks for the suggestion,

I like the idea. Unfortunately I don't have time to look into this myself at the moment, but I'd definitely appreciate a PR for this, so go ahead :+1:

HenningM avatar Jun 09 '16 14:06 HenningM

Hmm. The locals property is only really meant to be used for templating purposes (eg. to pass variables to the view engine) - that's why it exists on the res object. It's not meant as a generic 'request state container'.

The more common convention for passing data between middleware in Express is to simply store data in properties directly on the req or res objects (depending on what makes most sense semantically), without a specific 'namespace property' for that data.

This should already be possible with both req and ws objects when using express-ws, although the ws property will only be accessible from within middleware that's defined directly on the .ws route, not on the app/router-wide middleware. The req object, however, is shared between all middleware.

In short; I don't think this needs specific support from the express-ws side, since it can already be implemented according to common Express conventions, and adding something explicit to express-ws wouldn't yield any concrete benefits.

joepie91 avatar Mar 10 '17 12:03 joepie91