Home > Net >  Will res.locals work when using clusters?
Will res.locals work when using clusters?

Time:01-02

https://nodejs.org/api/cluster.html https://github.com/RGBboy/express-flash/blob/master/lib/express-flash.js

express-flash and connect-flash are just an add-on to res.locals. The problem with res.locals is that if I use cluster, then, in theory, the message has a probability of 1/(num of workers) not reaching the recipient. I can't test this, but it's an important thing. Is this true or am I wrong?

It turns out that the best way is to use req.session to send and receive messages?

CodePudding user response:

Will express-flash work with clustering?

It depends upon whether you're running clustering-compatible session middleware.

The express-flash module uses the connect-flash module which stores its flash message in req.session. So, for a future request (often a redirect) to be able to access the flash message, the user's session has to be preserved.

So, if you're using a session implementation that is compatible with clustering, then express-flash will also work with clustering. If you either don't have any session support configured or the session you're using is not clustering compatible (e.g. not based on some centralized store that all clustered processes can access), then express-flash will not work properly.

So, it depends upon whether you're running a clustering-compatible session or not.


FYI, this doesn't really have anything to do with res.locals as that's not where the flash data is stored (since that's a temporary object that exists only for the duration of one request). That's just the final place that the data is placed by the flash middleware when there's an incoming request and it's placed there so that templates can access it if they wish.

  • Related