快递4. app.locals,res.locals和req.app.locals有什么区别? [英] Express4. What's the difference between app.locals, res.locals and req.app.locals?

查看:157
本文介绍了快递4. app.locals,res.locals和req.app.locals有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Express 4时我很困惑.我使用 express-generator 生成我的项目.根文件中有 app.js ,路由器文件中有 index.js .但是,互联网上有关Express的教​​程直接在 app.js 中使用路由器.因此,当我想在路由器文件中的 index.js 中设置一些变量时,我使用了app.locals,但是它不起作用.但是当我更改为其他两个时,我的ejs模板可以工作了……我很困惑.有人可以告诉我它们之间的区别以及如何正确使用吗?

I am so confused while using express 4. I use express-generator to generate my project. And there are app.js in root and index.js in router file. However, the tutorial on internet about express are using router directly in app.js. So when I want to set some variables in index.js(in router file), I use app.locals, but it doesn't work. But when I change to the other two, my ejs template works... I am very confused. Anybody can tell me the difference between them and how to use correctly, please?

<!-- language: index.js in router file -->

    var app = require('express');
    var router = express.Router();

    ....

router.get('/', function(req, res, next) {
    var _user = req.session.user;
    if (_user) {
      //does't work!!
      //app.locals.user=_user;
      //I am not sure about which usage is correct below
      //1.
      req.app.locals.user = _user;
      //2.
      // res.locals.user=_user;
    }
}

<!-- language: lang-ejs -->

    <% if (user) { %>
      <li><a class="navbar-link">Welcome <%= user.name %></a>
      </li>
      <span>&nbsp;|&nbsp;</span>
      <li><a href="/logout" class="navbar-link" id="logoutBtn">Logout</a>
      </li>
      <% } else { %>
        <li><a href="#" class="navbar-link" data-toggle="modal" data-target="#signinModal">登录</a>
        </li>
        <span>&nbsp;|&nbsp;</span>
        <li><a href="#" class="navbar-link" data-toggle="modal" data-target="#signupModal">注册</a>
        </li>
      <% } %>

推荐答案

  • app.locals 对象是一个JavaScript对象,并且它的属性是应用程序中的局部变量.

    • The app.locals object is a JavaScript object, and its properties are local variables within the application.

      app.locals.title
      // => 'My App' 
      app.locals.email
      // => 'me@myapp.com'
      

      设置后,app.locals属性的值将在应用程序的整个生命周期中持续存在

      Once set, the value of app.locals properties persist throughout the life of the application

      res.locals属性相反,这些属性仅在请求的生存期内有效.当您处理具有res对象的路线时,那里将没有应用程序对象,反之亦然.对于app.locals.

      In contrast with res.locals properties that are valid only for the lifetime of the request. When you handle the route where you have a res object, you won't have an app object there and vice-versa for app.locals.

      您可以在应用程序内呈现的模板中访问局部变量.这对于为模板以及应用程序级数据提供帮助功能很有用.可以通过req.app.locals在中间件中使用本地语言(请参见 req.app )

      You can access local variables in templates rendered within the application. This is useful for providing helper functions to templates, as well as app-level data. Locals are available in middleware via req.app.locals (see req.app)

      app.locals.title = 'My App';
      app.locals.strftime = require('strftime');
      app.locals.email = 'me@myapp.com';
      

    • Node.js In Action书中的一张图片如下,描述了app.localres.local

      One picture from Node.js In Action book as below, describe the difference of app.local and res.local

      这篇关于快递4. app.locals,res.locals和req.app.locals有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆