Mar*_*tin 81 json mongoose node.js express
使用nodejs和express,我想使用JSON返回一个或多个对象(数组).在下面的代码中,我一次输出一个JSON对象.它有效,但这不是我想要的.由于我有很多对象,因此生成的响应不是有效的JSON响应.
我很清楚我可以简单地将所有对象添加到数组中并在res.end中返回该特定数组.但是,我担心这会对处理和内存密集而变得沉重.
用nodejs实现这个的正确方法是什么?query.each是正确的调用方法吗?
app.get('/users/:email/messages/unread', function(req, res, next) {
var query = MessageInfo
.find({ $and: [ { 'email': req.params.email }, { 'hasBeenRead': false } ] });
res.writeHead(200, { 'Content-Type': 'application/json' });
query.each(function(err, msg) {
if (msg) {
res.write(JSON.stringify({ msgId: msg.fileName }));
} else {
res.end();
}
});
});
Run Code Online (Sandbox Code Playgroud)
zob*_*225 182
在express 3上你可以直接使用res.json({foo:bar})
res.json({ msgId: msg.fileName })
Run Code Online (Sandbox Code Playgroud)
请参阅文档
dan*_*ugh 20
我不知道这是否真的有什么不同,但不是迭代查询光标,你可以这样做:
query.exec(function (err, results){
if (err) res.writeHead(500, err.message)
else if (!results.length) res.writeHead(404);
else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(results.map(function (msg){ return {msgId: msg.fileName}; })));
}
res.end();
});
Run Code Online (Sandbox Code Playgroud)
mae*_*ics 12
[编辑]在查看Mongoose文档后,看起来您可以将每个查询结果作为单独的块发送; Web服务器默认使用分块传输编码 ,因此您只需在项目周围包装一个数组,使其成为有效的JSON对象.
大致(未经测试):
app.get('/users/:email/messages/unread', function(req, res, next) {
var firstItem=true, query=MessageInfo.find(/*...*/);
res.writeHead(200, {'Content-Type': 'application/json'});
query.each(function(docs) {
// Start the JSON array or separate the next element.
res.write(firstItem ? (firstItem=false,'[') : ',');
res.write(JSON.stringify({ msgId: msg.fileName }));
});
res.end(']'); // End the JSON array and response.
});
Run Code Online (Sandbox Code Playgroud)
或者,正如您所提到的,您可以简单地按原样发送数组内容.在这种情况下,响应主体将被缓冲并立即发送,这可能消耗大量额外内存(高于存储结果本身所需的内存)以用于大型结果集.例如:
// ...
var query = MessageInfo.find(/*...*/);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(query.map(function(x){ return x.fileName })));
Run Code Online (Sandbox Code Playgroud)