简体   繁体   English

如何从 POST 请求中获取带有归档程序的压缩文件?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM