【问题标题】:Return zip file with node.js and express goes wrong返回带有 node.js 的 zip 文件和 express 出错
【发布时间】:2020-08-14 03:23:03
【问题描述】:

我无法使用 express 从 nodejs 的 http 响应中返回一个 zip 文件。我正在使用 ArchiverJS 和 azure-function-express。我一直在尝试解决这个问题一天,但我无法让它工作..

我的代码:

const createHandler = require("azure-function-express").createHandler;
const express = require("express");

// Create express app as usual
const app = express();


app.get("/api/download", (req, res) => {

    var Archiver = require('archiver');

    // Tell the browser that this is a zip file.
    res.writeHead(200, {
        'Content-Type': 'application/zip',
        'Content-disposition': 'attachment; filename=myFile.zip'
    });

    var zip = Archiver('zip');

    // Send the file to the page output.
    zip.pipe(res); // <--- GENERATES TypeError: Cannot read property 'length' of null 

    // Create zip with some files. Two dynamic, one static. Put #2 in a sub folder.
    zip.append('Some text to go in file 1.', { name: '1.txt' })
        .append('Some text to go in file 2. I go in a folder!', { name: 'somefolder/2.txt' })
        .file('staticFiles/3.txt', { name: '3.txt' })
        .finalize();

});
// Binds the express app to an Azure Function handler
module.exports = createHandler(app);

我对管道没有任何经验,但我认为这是它失败的地方..

这将产生以下错误,我敢打赌它与“zip.pipe(res)”有关。也许有人可以解释我做错了什么?

[error] Worker 50fea52b-2a26-4eee-bf1a-5956a3bda16f uncaught exception (learn more: https://go.microsoft.com/fwlink/?linkid=2097909 ): TypeError: Cannot read property 'length' of null  
   at ServerResponse._send (_http_outgoing.js:231:33 undefined)  
   at write_ .write_ (_http_outgoing.js:625:15 undefined)  
   at ServerResponse.write (_http_outgoing.js:567:10 undefined)  
   at Archiver.ondata (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_readable.js:681:20 undefined)  
   at Archiver.emit (events.js:198:13 undefined)  
   at addChunk .addChunk (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_readable.js:298:12 undefined)  
   at readableAddChunk .readableAddChunk (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_readable.js:280:11 undefined)  
   at Archiver.Readable.push (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_readable.js:241:10 undefined)  
   at Archiver.Transform.push (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_transform.js:139:32 undefined)  
   at Archiver.afterTransform (C:\Users\KENUBBE\Desktop\smarthelmet-ml\node_modules\readable-stream\lib\_stream_transform.js:88:10 undefined)

【问题讨论】:

  • 要解决这个问题,请尝试隔离错误。首先,制作一个程序,无需 express 即可将您的存档写入磁盘。
  • 另外,你以我看不懂的方式发布了堆栈跟踪
  • 抱歉,我更新了堆栈跟踪... zip.pipe(res); //
  • fs.createReadStream(${__dirname}/measurements_Person-${person}_OrderNumber-${orderNumber}.zip).pipe(res);

标签: node.js express archiverjs


【解决方案1】:

我制作了一个最小的工作示例,它与您的几乎相同。这个对我有用。它使用:

node@10.16.0

express@4.17.1

archiver@4.0.1

const express = require("express");
// Create express app as usual
const app     = express();
const http    = require('http');
const server  = http.createServer(app);


app.get("/api/download", (req, res) => {

    var Archiver = require('archiver');

    // Tell the browser that this is a zip file.
    res.writeHead(200, {
        'Content-Type': 'application/zip',
        'Content-disposition': 'attachment; filename=myFile.zip'
    });

    var zip = Archiver('zip');

    // Send the file to the page output.
    zip.pipe(res); // <--- GENERATES TypeError: Cannot read property 'length' of null 

    // Create zip with some files. Two dynamic, one static. Put #2 in a sub folder.
    zip.append('Some text to go in file 1.', { name: '1.txt' })
        .append('Some text to go in file 2. I go in a folder!', { name: 'somefolder/2.txt' })
        .file('staticFiles/3.txt', { name: '3.txt' })
        .finalize();

});

server.listen(7070);

【讨论】:

  • 嗯好的,所以一切正常,因为 server.listen(7070).. 但是为什么呢? :O
  • 这在您的本地机器上工作吗?我刚刚扔掉了“azure-function-express”,因为我不习惯它
  • 另外,模块版本可能存在差异,这就是我发布我使用的版本的原因
  • 这在我的本地机器上工作,现在我也必须在 azure 函数中尝试它。azure-function-express 是一个库,用于在 azure 函数中使用 node.js 和 express 而不是本机 javascript
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-28
  • 2019-05-25
  • 2016-04-04
  • 1970-01-01
  • 2017-12-27
  • 2011-08-16
  • 1970-01-01
相关资源
最近更新 更多