Thursday 21 February 2019

[ Express JS] Maintaining middlewares with side-effects

Hey! I've been working with express.js for the past two years now, and one thing that I found to be quite difficult is how to properly document and enforce the state of the req object when multiple middlewares are mutating it. For example, I might have a validation middleware that updates the req.body object so that it only contains valid inputs. Another middleware might fetch an account from the database based on the id in req.body.id, and place it inside of req.body.account.When chaining these middlewares together, there is nothing that is preventing me from improperly chaining the middlewares together, with the exception of diligent coding.Has this been an issue for you all? If so, how do you manage this?A toy example is here: https://gist.github.com/pierreTklein/feca0766e4433ebb880a4558ac2a0706

Submitted February 21, 2019 at 08:12PM by ptklein

No comments:

Post a Comment