我正在尝试使用request.js检索file.csv.gz,并在解析和处理之前对其进行解压缩。
我知道我希望我的代码看起来像这样
response
.pipe(zlib.createGunzip())
.pipe(split())
.pipe(process())
但是,我正在努力让我的响应对象以正确的格式进行管道传输。我目前正试图通过这样做来传播响应;
const request = require('request');
const headers = {
'accept-encoding':'gzip'
}
module.exports.getCSV = (url) => {
return request({url, headers, gzip:true});
}
我收到的错误暗示我试图解压缩不完整的对象。
我也在想,也许不可能做我想要实现的目标,而是在尝试解析文件以进行处理之前我需要完全下载文件
答案 0 :(得分:0)
receive.js
const http = require('http');
const fs = require('fs');
const zlib = require('zlib');
const server = http.createServer((req, res) => {
const filename = req.headers.filename;
console.log('File request received: ' + filename);
req
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream(filename))
.on('finish', () => {
res.writeHead(201, {'Content-Type': 'text/plain'});
res.end('Complete\n');
console.log(`File saved: ${filename}`);
});
});
server.listen(3000, () => console.log('Listening'));
send.js
const fs = require('fs');
const zlib = require('zlib');
const http = require('http');
const path = require('path');
const file = 'myfile.txt'; //Put your file here
const server = 'localhost'; //Put the server here
const options = {
hostname: server,
port: 3000,
path: '/',
method: 'PUT',
headers: {
filename: path.basename(file),
'Content-Type': 'application/octet-stream',
'Content-Encoding': 'gzip'
}
};
const req = http.request(options, res => {
console.log('Server response: ' + res.statusCode);
});
fs.createReadStream(file)
.pipe(zlib.createGzip())
.pipe(req)
.on('finish', () => {
console.log('File successfully sent');
});