最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS res.on end is not triggered - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 2

Given that your redisc.smembers query is async in nature, you will have to make the following edits:

  1. 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.

  2. Remove the res.send() from inside the res.on("end") block.

  3. 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.

发布评论

评论列表(0)

  1. 暂无评论