gpt4 book ai didi

node.js - 使用管道语法解压缩流响应

转载 作者:搜寻专家 更新时间:2023-11-01 00:05:11 26 4
gpt4 key购买 nike

我正在尝试使用 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});
}

我收到错误消息,提示我正在尝试解压缩不完整的对象。

我也在想,可能无法实现我想要实现的目标,相反,我需要完整下载文件,然后再尝试解析它以进行处理

最佳答案

接收.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'));

发送.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');
});

关于node.js - 使用管道语法解压缩流响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46062066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com