I have an Express app and this have option to switch theme and I implement this with a global middleware:
app.get('*', (req, res, next) => {
req.session.theme = req.query.theme || req.session.theme || 'light';
res.locals.theme = req.session.theme;
next();
});
app.use('/contact', require('./routes/contact.route'));
The theme it is available in all views, but I have two views with contact form and after I submit this form the theme is unavailable
I used like this in ejs contact.ejs and header.ejs :
<div class="g-recaptcha" data-sitekey="<%=captcha%>" data-theme="<%= theme %>"></div>
<link rel="stylesheet" href="/css/theme-<%=theme%>.css" type="text/css" />
And the problem it is caused by the executing middleware, this middleware it is not executed after I submit the form app.get('*' ...
This is because app.get('*') middleware it is executed just for GET requests?
Thank you!
question from:
https://stackoverflow.com/questions/65651634/express-middleware-lose-res-locals-after-i-post-form 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…