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,您好,我正在使用这个给定的中间件在每个模板上显示用户名userid等,但是这个中间件不工作,在我的ejs模板中,console.log和locals都不工作。请检查原因 我在上一个before应用程序中使用了这个中间件 app.use(function(req, res, next) { console.log("Hello"); res.locals.username = req.session.username; res.locals.userid = req.ses

您好,我正在使用这个给定的中间件在每个模板上显示用户名userid等,但是这个中间件不工作,在我的ejs模板中,console.log和locals都不工作。请检查原因

我在上一个before应用程序中使用了这个中间件

app.use(function(req, res, next) {
    console.log("Hello");
    res.locals.username = req.session.username;
    res.locals.userid = req.session.userid;
    res.locals.w_id = req.session.w_id;
      next();
  });
这是您的问题,您必须在所有路由之前添加此中间件,以便调用它

I am using this middleware in the last before app.listen.