Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在我发布表单后,请快速删除res.locals_Node.js_Express_Ejs - Fatal编程技术网

Node.js 在我发布表单后,请快速删除res.locals

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/

我有一个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请求执行它


谢谢!

当您的表单发送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*/)