我对如何通过管道传输一些数据感到有点困惑。
我有一些工作和链接的管道,所以我没有包含我想要输入到请求POST的数据的输出流
var options = {
host: 'localhost',
port: 8529,
path: '/_api/cursor',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
}
var req = http.request(options);我通常只会操作‘mystreams2.pip(Req)’,但是我该如何设置'data.length‘的值呢?
(我使用的是streams2接口,而不是旧的流格式)
发布于 2013-09-30 07:57:47
假设你的缓冲区中没有大量的数据,你首先需要收集数据以便找到它的长度。
var source = /* a readable stream */;
var data = '';
source.on('data', function(chunk) {
data += chunk;
});
source.on('end', function() {
var options = {
host: 'localhost',
port: 8529,
path: '/_api/cursor',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
};
var req = http.request(options);
req.end(data);
});https://stackoverflow.com/questions/19079523
复制相似问题