Javascript http.createServer如何使用fs.createReadStream从needle.post接收数据

Javascript http.createServer如何使用fs.createReadStream从needle.post接收数据,javascript,node.js,Javascript,Node.js,我想将一个映像从一个nodejs服务器发送到另一个nodejs服务器 我知道有很多解决方案,但我希望通过以下方式找到解决方法: 服务器A(发送方) 选择1 needle.post('http://127.0.0.1:8000', fs.createReadStream('test.png'), function(err, resp, body) { }); 选择2 var reader = fs.createReadStream('test.png'); read

我想将一个映像从一个nodejs服务器发送到另一个nodejs服务器

我知道有很多解决方案,但我希望通过以下方式找到解决方法:

服务器A(发送方)

选择1

needle.post('http://127.0.0.1:8000', fs.createReadStream('test.png'), function(err, resp, body) {               
});
选择2

var reader = fs.createReadStream('test.png');
reader.pipe(request.post('http://127.0.0.1:8000'));
服务器B(接收器)

问题是,如果我读取使用fs.createReadStream发送的文件数据,我将无法从服务器B接收任何数据


[编辑]还需要知道如何使用Express处理上述问题,正如zangw在评论中提到的那样,脚本实际上是有效的。我的缺点是My test.png为空

您可以尝试创建fs.createWriteStream()并分配给req.pipe()


使用
console.log(分块)
在('data')的
req.on和服务器A的选项1中,它在我的本地测试中运行良好…我的坏,我刚刚意识到我的test.png是blank谢谢,如果使用Express处理它,接收端的行为也会有所不同。忽略此答案,犯下另一个错误谢谢,使用Express时,我可以使用上述语句接收
http.createServer(function(req, res) {
  if (req.method === 'PUT' || req.method === 'POST') {
     req.on('data', function(chunked) {
        // I got nothing here
     });
     res.on('data', function(chunked) {
        // I got nothing here
     });
  }
}).listen(8000, function() {
  console.log('Listening for requests');
});
...
var saveTo = './test.png',
    ws = fs.createWriteStream(saveTo);

ws.on('close', function () {
    cb();
});
req.pipe(ws);
...