【发布时间】:2021-01-10 17:24:57
【问题描述】:
我有一个 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/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 请求执行的吗?
谢谢!
【问题讨论】:
-
既然您要发布表单,您是否尝试过将
app.get('*',/* rest */)更改为app.post('*',/* rest */)?