Express js app.get回调中如何获取多个独立的响应数据
How to get more than one independent response data in Express js app.get callback
通过 HTTP 方法在 Express 应用程序中发送两个独立的 MongoDB 结果的最佳做法是什么?
这里有一个简短的例子,可以清楚地说明:
//app.js
var express = require('express');
var app = express();
var testController = require('./controllers/test');
app.get('/test', testController.getCounts);
...
以下 getCounts() 函数将不起作用,因为我无法发送两次响应。
///controllers/test
exports.getCounts = function(req,res) {
Object1.count({},function(err,count){
res.send({count:count});
});
Object2.count({},function(err,count){
res.send({count:count});
});
};
无论如何,我想在一个响应对象中包含这两个计数。
我是否应该在 Object1 的回调中调用 Object2.count,即使它们彼此不依赖?
或者我应该以其他方式重新设计它?
谢谢!
您应该使用 Promise 来完成此任务:
function getCount(obj) {
return new Promise(function (resolve, reject) {
obj.count({}, function(err,count) {
if(err) reject();
else resolve(count);
});
});
}
使用 Promise.all
您可以触发两个请求并检索结果以便将其添加到响应中
exports.getCounts = function(req,res) {
Promise.all([getCount(Object1), getCount(Object2)])
.then(function success(result) {
res.send({'count1':result[0], 'count2':result[1]});
});
});
当您调用 res.send
时,您将结束对请求的响应。您可以改为使用 res.write
,它将向客户端发送一个块,完成后调用 res.end
;
示例:
app.get('/endpoint', function(req, res) {
res.write('Hello');
res.write('World');
res.end();
});
但是,您似乎正试图将 json 发送回客户端,这引发了问题:单独写入对象将无效 json。
示例:
app.get('/endpoint', function(req, res) {
res.write({foo:'bar'});
res.write({hello:'world'});
res.end();
});
响应正文现在将是:{foo:'bar'}{hello:'world'}
,这是无效的 json。
两个数据库查询之间也会出现竞争条件,这意味着您不确定响应中数据的顺序。
建议:
exports.getCounts = function(req,res) {
var output = {};
Object1.count({},function(err,count){
output.count1 = count;
Object2.count({},function(err,count){
output.count2 = count;
res.send(output);
});
});
};
//Response body
{
count1: [value],
count2: [value]
}
通过 HTTP 方法在 Express 应用程序中发送两个独立的 MongoDB 结果的最佳做法是什么?
这里有一个简短的例子,可以清楚地说明:
//app.js
var express = require('express');
var app = express();
var testController = require('./controllers/test');
app.get('/test', testController.getCounts);
...
以下 getCounts() 函数将不起作用,因为我无法发送两次响应。
///controllers/test
exports.getCounts = function(req,res) {
Object1.count({},function(err,count){
res.send({count:count});
});
Object2.count({},function(err,count){
res.send({count:count});
});
};
无论如何,我想在一个响应对象中包含这两个计数。
我是否应该在 Object1 的回调中调用 Object2.count,即使它们彼此不依赖?
或者我应该以其他方式重新设计它?
谢谢!
您应该使用 Promise 来完成此任务:
function getCount(obj) {
return new Promise(function (resolve, reject) {
obj.count({}, function(err,count) {
if(err) reject();
else resolve(count);
});
});
}
使用 Promise.all
您可以触发两个请求并检索结果以便将其添加到响应中
exports.getCounts = function(req,res) {
Promise.all([getCount(Object1), getCount(Object2)])
.then(function success(result) {
res.send({'count1':result[0], 'count2':result[1]});
});
});
当您调用 res.send
时,您将结束对请求的响应。您可以改为使用 res.write
,它将向客户端发送一个块,完成后调用 res.end
;
示例:
app.get('/endpoint', function(req, res) {
res.write('Hello');
res.write('World');
res.end();
});
但是,您似乎正试图将 json 发送回客户端,这引发了问题:单独写入对象将无效 json。
示例:
app.get('/endpoint', function(req, res) {
res.write({foo:'bar'});
res.write({hello:'world'});
res.end();
});
响应正文现在将是:{foo:'bar'}{hello:'world'}
,这是无效的 json。
两个数据库查询之间也会出现竞争条件,这意味着您不确定响应中数据的顺序。
建议:
exports.getCounts = function(req,res) {
var output = {};
Object1.count({},function(err,count){
output.count1 = count;
Object2.count({},function(err,count){
output.count2 = count;
res.send(output);
});
});
};
//Response body
{
count1: [value],
count2: [value]
}