Node.js 在我发布表单后,请快速删除res.locals
我有一个Express应用程序,可以选择切换主题,我用一个全局中间件实现了这一点:Node.js 在我发布表单后,请快速删除res.locals,node.js,express,ejs,Node.js,Express,Ejs,我有一个Express应用程序,可以选择切换主题,我用一个全局中间件实现了这一点: 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/
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'));
主题在所有视图中都可用,但我有两个带有联系人表单的视图,提交此表单后,主题不可用
我在ejs contact.ejs和header.ejs中使用了如下内容:
<div class="g-recaptcha" data-sitekey="<%=captcha%>" data-theme="<%= theme %>"></div>
<link rel="stylesheet" href="/css/theme-<%=theme%>.css" type="text/css" />
这个问题是由正在执行的中间件引起的,这个中间件在我提交表单app.get('*')后没有执行
这是因为app.get(“*”)中间件仅针对get请求执行它
谢谢!当您的表单发送post或put请求时,您的
app.get()
不会在表单提交中执行
把你的代码改成这个
app.use( (req, res, next) => {
req.session.theme = req.query.theme || req.session.theme || 'light';
res.locals.theme = req.session.theme;
next();
});
我希望这能解决问题,如果还没有,请让我知道。既然您正在发布表单,您是否尝试过使用
app.get('*',/*rest*/)更改app.post('*',/*rest*/)
?