【问题标题】:Stream application/json POST request流应用程序/json POST 请求
【发布时间】:2016-07-30 08:53:14
【问题描述】:

我有工作代码来缓冲 .json 文件,然后将该数据 POST 到服务器。

  fs.readFile(filePath, 'utf-8', function (err, buf) {

        if(err){
            reject(err);
        }
        else{
            const req = http.request({
                method: 'POST',
                host: 'localhost',
                path: '/event',
                port: '4031',
                headers: {
                    'Content-Type': 'application/json',
                    'Content-Length': buf.length
                }
            });


            req.on('error', reject);

            var data = '';

            req.on('response', function (res) {

                res.setEncoding('utf8');

                res.on('data', function ($data) {
                    data += $data
                });

                res.on('end', function () {

                    data = JSON.parse(data);
                    console.log('data from SC:', data);

                    //call fn on data and if it passes we are good
                    resolve();
                });
            });

            // write data to request body
            req.write(buf);
            req.end();
        }


    });

我想做的是避免缓冲它,而只使用 fs.createReadStream,就像这样:

  fs.createReadStream(filePath, 'utf-8', function (err, strm) {

    if(err){
        reject(err);
    }
    else{
        const req = http.request({
            method: 'POST',
            host: 'localhost',
            path: '/event',
            port: '4031',
            headers: {
                'Content-Type': 'application/json',
                // 'Content-Length': buf.length
            }
        });


        req.on('error', reject);

        var data = '';

        req.on('response', function (res) {

            res.setEncoding('utf8');

            res.on('data', function ($data) {
                data += $data
            });

            res.on('end', function () {

                data = JSON.parse(data);
                console.log('data from SC:', data);

                //call fn on data and if it passes we are good
                resolve();
            });
        });

        // write data to request body
        req.write(strm);
        req.end();
    }


});

但这并不完全有效?可以这样做吗?

【问题讨论】:

    标签: node.js http post fs


    【解决方案1】:

    这似乎有效,但不确定它是否 100% 正确

    const strm = fs.createReadStream(filePath);
    
    
    const req = http.request({
        method: 'POST',
        host: 'localhost',
        path: '/event',
        port: '4031',
        headers: {
            'Content-Type': 'application/json',
        }
    });
    
    
    req.on('error', reject);
    
    var data = '';
    
    req.on('response', function (res) {
    
        res.setEncoding('utf8');
    
        res.on('data', function ($data) {
            data += $data
        });
    
        res.on('end', function () {
    
            data = JSON.parse(data);
            resolve();
        });
    });
    
    // write data to request body
    strm.pipe(req);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多