In express, when I use routing middleware, is it OK to append to the request object? Or is it a bad pattern? Alternatives? Thanks.
app.get('/', getLayout, function(req, res){
if(req.layout == 'simple') ...render simple layout...
else ...render full layout...
});
where
getLayout = function(req, res, next){
req.layout = (req.params.blah == 'blah') ? 'layout_simple' : 'layout_full';
next();
}
I don’t see why you shouldn’t.
I do it a lot.
I am under the impression this is what middleware typically does.
From the express docs:
https://expressjs.com/en/guide/writing-middleware.html
They set req.user in their middleware as the current user.