node.js 可以在 Express.js 的中间件模块中向响应对象添加数据吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11337402/
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
Is it OK to add data to the response object in a middleware module in Express.js?
提问by BFree
Here's the basic setup. I'm trying to create a simple middleware component that would allow me to easily pass data from my route directly to my javascript in the client side. (Very similiar to the Gongem in ruby). The way I'm doing it is by having a module that looks like this:
这是基本设置。我正在尝试创建一个简单的中间件组件,它可以让我轻松地将数据从我的路由直接传递到客户端的 javascript。(非常类似于红宝石中的Gon宝石)。我这样做的方式是拥有一个看起来像这样的模块:
module.exports = function(){
return function(req,res,next){
var app = req.app;
if(typeof(app) == 'undefined'){
var err = new Error("The JShare module requires express");
next(err);
return;
}
res.jshare = {};
app.dynamicHelpers({
includeJShare: function(req,res){
if(typeof(res.jshare) === 'undefined'){
return "";
}
return function(){
return '<script type="text/javascript">window.jshare=' + JSON.stringify(res.jshare) + '</script>';
}
}
});
next();
};
}
Then, in my route I can do this:
然后,在我的路线中,我可以这样做:
exports.index = function(req, res){
res.jshare.person = {firstName : "Alex"};
res.render('index', { title: 'Express' })
};
Finally in the layout.jade:
最后在 layout.jade 中:
!{includeJShare()}
What that does is in outputs a line of javascript on the client that creates the exact JSON object that was created server side.
它所做的是在客户端上输出一行 javascript,它创建了在服务器端创建的确切 JSON 对象。
Here's the question; it all works as expected, but being new to Express and Node.js in general, I was just curious if attaching properties onto the response object is OK, or is there something wrong with doing it that I'm simply overlooking? For some reason it doesn't pass my "smell test" but I'm not sure why.....
这是问题; 这一切都按预期工作,但总的来说,我是 Express 和 Node.js 的新手,我只是好奇,如果将属性附加到响应对象上是可以的,或者我只是忽略了这样做有什么问题吗?出于某种原因,它没有通过我的“气味测试”,但我不确定为什么.....
采纳答案by hasanyasin
It is perfectly OK. It is how JavaScript is designed. Only thing you should be careful is to not accidentally overriding already existing properties or being overridden by others. To be safer, instead of adding everything directly to req/res objects, you might consider going a level deeper:
完全没问题。这就是 JavaScript 的设计方式。您唯一应该注意的是不要意外覆盖已经存在的属性或被其他人覆盖。为了更安全,不要将所有内容直接添加到 req/res 对象,您可能会考虑更深一层:
res.mydata={}
res.mydata.person= ...
Like that.
像那样。
回答by Brandon
I know this is an old thread, but there is something else to add to this topic.
我知道这是一个旧线程,但还有其他内容要添加到此主题中。
Express has a response.locals object which is meant for this purpose - extending the response from middleware to make it available to views.
Express 有一个 response.locals 对象,用于此目的 - 扩展来自中间件的响应以使其可用于视图。
You could add a property directly to the response object, and as @hasanyasin indicated, is how JavaScript is designed. But Express, more specifically, has a particular way they prefer we do it.
您可以直接向响应对象添加一个属性,正如@hasanyasin 指出的那样,这就是 JavaScript 的设计方式。但是,更具体地说,Express 有一种他们更喜欢我们这样做的特殊方式。
This may be new in express 3.x, not sure. Perhaps it didn't exist when this question was asked.
这可能是 express 3.x 中的新功能,不确定。也许在问这个问题时它不存在。
For details, see
有关详细信息,请参阅
http://expressjs.com/en/api.html#res.locals
http://expressjs.com/en/api.html#res.locals
There is also an app.locals for objects which don't vary from request to request (or response to response I suppose).
还有一个 app.locals 对象,它不会因请求而异(或我想对响应的响应)。

