[英]How can I get a compressed file with archiver from a POST request?
I am building a NodeJS API
with Express where when you make a POST
, it generates a TAR
file based on the body of the request.我正在使用 Express 构建一个
NodeJS API
,当您进行POST
,它会根据请求正文生成一个TAR
文件。
Problem:
问题:
When the endpoint is a POST
, I have access to the body of the request, and can seemingly make things with it.当端点是
POST
,我可以访问请求的正文,并且似乎可以用它来做事情。 But, I can't see/use/test a compressed file from that (as far as I can tell).但是,我无法从中看到/使用/测试压缩文件(据我所知)。
When the endpoint is a GET
, I don't have access to the body of the request (as far as I can tell), but I can query the URL in the browser and get the compressed file.当端点是
GET
,我无权访问请求的正文(据我所知),但我可以在浏览器中查询 URL 并获取压缩文件。
Basically I want to solve one of the “as far as I can tell's.基本上我想解决“据我所知”之一。 This is my relevant code so far:
到目前为止,这是我的相关代码:
const fs = require('fs');
const serverless = require('serverless-http');
const archiver = require('archiver');
const express = require('express');
const app = express();
const util = require('util');
app.use(express.json());
app.post('/', function(req, res) {
var filename = 'export.tar';
var output = fs.createWriteStream('/tmp/' + filename);
output.on('close', function() {
res.download('/tmp/' + filename, filename);
});
var archive = archiver('tar');
archive.pipe(output);
// This part does not work when this is a GET request.
// The log works perfectly in a POST request, but I can't get the TAR file from the command line.
res.req.body.files.forEach(file => {
archive.append(file.content, { name: file.name });
console.log(`Appending ${file.name} file: ${JSON.stringify(file, null, 2)}`);
});
// This part is dummy data that works with a GET request when I go to the URL in the browser
archive.append(
"<h1>Hello, World!</h1>",
{ name: 'index.html' }
);
archive.finalize();
});
Sample JSON body data that I send to this:我发送给这个的示例 JSON 正文数据:
{
"title": "Sample Title",
"files": [
{
"name": "index.html",
"content": "<p>Hello, World!</p>"
},
{
"name": "README.md",
"content": "# Hello, World!"
}
]
}
I'm just supposed to send JSON
and get a TAR based on the SON
.我只是应该发送
JSON
并获得基于SON
的 TAR 。 Is POST
the wrong method for this? POST
是错误的方法吗? If I use GET
, what should change so I can use that JSON
data?如果我使用
GET
,应该改变什么才能使用该JSON
数据? Is there a way to "daisy chain" requests (that seems unclean, but maybe the solution)?有没有办法“菊花链”请求(这看起来不干净,但也许是解决方案)?
Try this:尝试这个:
app.post('/', (req, res) => {
const filename = 'export.tar';
const archive = archiver('tar', {});
archive.on('warning', (err) => {
console.log(`WARN -> ${err}`);
});
archive.on('error', (err) => {
console.log(`ERROR -> ${err}`);
});
const files = req.body.files || [];
for (const file of files) {
archive.append(file.content, { name: file.name });
console.log(`Appending ${file.name} file: ${JSON.stringify(file, null, 2)}`);
}
try {
if (files.length > 0) {
archive.pipe(res);
archive.finalize();
return res.attachment(filename);
} else {
return res.send({ error: 'No files to be downloaded' });
}
} catch (e) {
return res.send({ error: e.toString() });
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.