我想从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(接收机)
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处理上面的内容
发布于 2016-02-18 08:52:27
您可以尝试创建fs.createWriteStream()并将其分配给req.pipe()。
...
var saveTo = './test.png',
ws = fs.createWriteStream(saveTo);
ws.on('close', function () {
cb();
});
req.pipe(ws);
...发布于 2016-02-18 05:35:10
正如zangw在注释中所提到的,这个脚本实际上是有效的。我的错是我的test.png是空白的
https://stackoverflow.com/questions/35472515
复制相似问题