I'm having a problem in end the responded data to the client. The response is not sent to the client, res.on('end') is not triggered. I don't have any idea to make this things work out. My goal is to send the retrieved data in redis to the client. Is there any way to make this work?. By the way, here is my code guys. Thanks
app.get('/contacts/list', function(req, res) {
var someId = null;
var someData;
var optionsA = {
hostname: host,
path: checkHeaders,
headers: {
'Content-Type': 'application/json',
'someheaders1': req.headers.someheaders1,
'someheaders2': req.headers.someheaders2,
'someheaders3': req.headers.someheaders3,
'someheaders4': req.headers.someheaders4,
'user-agent': req.headers['user-agent'],
'local': req.header('x-forwarded-for') || req.connection.remoteAddress
}
};
var req = https.get(optionsA, function(res) {
res.on('data', function(chunk) {
if (res.statusCode === 200) {
userId = chunk;
redisc.smembers('setId:' + someId, function(err, data) {
if (!err) {
someData = data;
} else {
console.log(err);
}
});
} else {
console.log('404');
}
});
res.on('end', function() {
res.send(someData);
console.log('end');
});
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});
res.end();
});
Thanks in advance. Guys.
I'm having a problem in end the responded data to the client. The response is not sent to the client, res.on('end') is not triggered. I don't have any idea to make this things work out. My goal is to send the retrieved data in redis to the client. Is there any way to make this work?. By the way, here is my code guys. Thanks
app.get('/contacts/list', function(req, res) {
var someId = null;
var someData;
var optionsA = {
hostname: host,
path: checkHeaders,
headers: {
'Content-Type': 'application/json',
'someheaders1': req.headers.someheaders1,
'someheaders2': req.headers.someheaders2,
'someheaders3': req.headers.someheaders3,
'someheaders4': req.headers.someheaders4,
'user-agent': req.headers['user-agent'],
'local': req.header('x-forwarded-for') || req.connection.remoteAddress
}
};
var req = https.get(optionsA, function(res) {
res.on('data', function(chunk) {
if (res.statusCode === 200) {
userId = chunk;
redisc.smembers('setId:' + someId, function(err, data) {
if (!err) {
someData = data;
} else {
console.log(err);
}
});
} else {
console.log('404');
}
});
res.on('end', function() {
res.send(someData);
console.log('end');
});
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});
res.end();
});
Thanks in advance. Guys.
Share Improve this question edited Mar 7, 2014 at 7:30 loganfsmyth 162k31 gold badges346 silver badges258 bronze badges asked Mar 7, 2014 at 5:07 user3012847user3012847 511 gold badge1 silver badge10 bronze badges1 Answer
Reset to default 2Given that your redisc.smembers
query is async in nature, you will have to make the following edits:
Change the res ivar in here to something like _res so it doesn't conflict with the res we have for our main request:
var req = https.get(optionsA, function(_res) {
. Make similar changes elsewhere inside the https.get block as well.Remove the
res.send()
from inside theres.on("end")
block.Now for the https.get and redis block
var _req = https.get(optionsA, function(_res) { _res.on('data', function(chunk) { if (_res.statusCode === 200) { userId = chunk; redisc.smembers('setId:' + someId, function(err, data) { if (!err) { res.send(data); //you probably want to wrap data inside JSON.stringify() res.end(); } else { console.log(err); } }); } else { console.log('404'); } }); _res.on('end', function() { console.log('end'); }); }); _req.on('error', function(e) { console.log('ERROR: ' + e.message); }); res.end()// Remove this line. It is prematurely closing your connection without sending any data. I've only included it here to show what needs to be done.
Your current code is probably not working since you have two res
ivars in your code and node thinks the new res is the one where you want to send the data.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745348982a4623712.html
评论列表(0条)