在node.js(Express)中分配给res和res.locs之间的区别 [英] Difference between assigning to res and res.locals in node.js (Express)

查看:95
本文介绍了在node.js(Express)中分配给res和res.locs之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在一个代码示例中学习nodejs有一个中间件(messages.js),一个服务器(app.js)和模板(messages.ejs)。查看模板的示例代码。看来,尽管消息和removeMessages()被分配给res.locals。您可以使用消息或removeMessages()访问它们,而无需使用本地人对该呼叫作前缀。我想知道以下内容:


  1. 他们是否指向相同的对象?

  2. 如果他们如果分配给res直接而不是res.locals,那么是一样的吗?

示例代码



messages.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(部分代码)

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

messages.ejs

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


解决方案

res.locals 是传递给您的应用程序正在使用的任何渲染引擎的对象(在这种情况下为 ejs )。他们将在渲染中成为全局,所以您不需要为他们添加任何东西来使用它们。



说我们希望我们的服务器能够我们的JavaScript从S3处于生产模式,但在开发时使用本地副本。 res.locals 使这很容易。我们在app.js中有这样的中间件:

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

index.ejs 将是这样的:

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


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

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:

  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?

Sample Code

messages.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)

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

messages.ejs

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

解决方案

res.locals is 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.

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.locals makes this easy. We'd have middleware along these lines in app.js:

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

and index.ejs would be something like this:

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

这篇关于在node.js(Express)中分配给res和res.locs之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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