http.createServer如何使用fs.createReadStream从needle.post接收数据
我想从一个nodejs服务器发送一个图像到另一个nodejs服务器。
我知道有很多的解决scheme,但我希望找出如何做到这一点在以下方面:
服务器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(接收器)
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'); });
问题是,如果我读取使用fs.createReadStream发送的文件数据,我无法从服务器B接收任何数据。
[编辑]还需要知道如何处理上面使用Express
您可以尝试创buildfs.createWriteStream()并将其分配给req.pipe()。
... var saveTo = './test.png', ws = fs.createWriteStream(saveTo); ws.on('close', function () { cb(); }); req.pipe(ws); ...
正如zangw在评论中提到的,这个脚本实际上是有效的。 我的不好,我的test.png是空白的