javascript 在 node.js (Express) 中分配给 res 和 res.locals 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24072333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:08:33  来源:igfitidea点击:

Difference between assigning to res and res.locals in node.js (Express)

javascriptnode.jsexpress

提问by seetdev

Hi I have some newbie questions about the use of res (Express response object) and res.locals in Express.

嗨,我有一些关于在 Express 中使用 res(Express 响应对象)和 res.locals 的新手问题。

While studying nodejs in one of the code examples There is a middleware (messages.js), a server (app.js) and the template (messages.ejs). Looking into the sample code for the template. It appears that although messages and removeMessages() is assigned to res.locals. You can access them using messages or removeMessages() without prefixing the call with locals. I wish to know the following:

在其中一个代码示例中研究 nodejs 时,有一个中间件(messages.js)、一个服务器(app.js)和模板(messages.ejs)。查看模板的示例代码。看来虽然messages 和removeMessages() 被分配给res.locals。您可以使用消息或 removeMessages() 访问它们,而无需在调用前加上本地人。我想知道以下内容:

  1. Are they pointing to the same objects?
  2. If they are the same does it matter if I assign to res direct instead of res.locals?
  1. 它们指向相同的对象吗?
  2. 如果它们相同,我分配给 res direct 而不是 res.locals 有关系吗?

Sample Code

示例代码

messages.js

消息.js

var express = require('express');
var res = express.response;
res.message = function (msg, type) {
    type = type || 'info'
    var sess = this.req.session;
    sess.messages = sess.messages || [];
    sess.messages.push({
        type: type,
        string: msg
    });
};
res.error = function (msg) {
    return this.message(msg, 'error');
};
module.exports = function (req, res, next) {
    res.locals.messages = req.session.messages || [];
    res.locals.removeMessages = function () {
        req.session.messages = [];
    };
    next();
};

app.js(partial code)

app.js(部分代码)

var express = require('express');
var messages = require('./lib/messages');
var app = express();
app.use(messages);

messages.ejs

消息.ejs

<% if(locals.messages) { %>
    <% messages.forEach(function (message) { % %>
        <p class = '<%= message.type %>' > <%= message.string %> < /p>
    <% }) %>
    <% removeMessages(); %>
<% } %>

回答by SomeKittens

res.localsis an object passed to whatever rendering engine your app is using (in this case ejs). They'll be 'global' in the render, so you don't need to prepend anything on to them to use them.

res.locals是传递给您的应用程序使用的任何渲染引擎的对象(在本例中ejs)。它们在渲染中将是“全局的”,因此您无需在它们之前添加任何内容即可使用它们。

Say we wanted our server to pick up our JavaScript from S3 when in production mode, but use the local copies when in development. res.localsmakes this easy. We'd have middleware along these lines in app.js:

假设我们希望我们的服务器在生产模式下从 S3 获取我们的 JavaScript,但在开发时使用本地副本。 res.locals使这变得容易。我们将在 app.js 中按照以下方式提供中间件:

if ('production' === app.get('env')) {
  res.locals.jsLocation = 'https://s3.amazonaws.com/kittens/js/'
} else {
  res.locals.jsLocation = '/js/';
}

and index.ejswould be something like this:

并且index.ejs会是这样的:

<script src="<%= jsLocation %>angular.min.js"></script>
<script src="<%= jsLocation %>myAngularFile.js"></script>