Return 带有 node.js 的 zip 文件和 express 出错

Return zip file with node.js and express goes wrong

我无法通过 express 从 nodejs 的 http 响应中 return 一个 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)

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

node@10.16.0

快递@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);