nodejs - 在http请求完成后使用res.send

时间:2018-06-16 14:37:00

标签: node.js http express

我正在使用带有express的节点,我正在我的节点服务器中调用一个http get请求 - 现在当完整数据从http请求到达时,我想用接收到的数据调用res.send。 我的代码如下:

app.get('/', function (req, res) {

const x = https.get(options, (req) => {
    console.log('statusCode:', req.statusCode);
    console.log('headers:', req.headers);

    req.on('data', (locations) => {
        buffer = new Buffer(locations);
        console.log(buffer);
        res.send(buffer);      <== should sent complete result
    });

}).on('error', (err) => console.log('error:', err));

x.end();
});

app.listen(port, function () {
    console.log('Example app listening on port 3000!');
});

我的代码问题: 我每次都得到不同大小的数据 - 基本上 - 每次都是一组不完整的数据。

0 个答案:

没有答案