Express:下载返回空文件

时间:2018-05-05 10:06:56

标签: javascript node.js express

每当我点击端点时,我都会在本地生成的文件正确并且包含我需要的所有字段时保持空文件。我已经尝试直接用于响应,但我会得到:

  

不能管道,不可读

当然可能有些事我做错了。

Similar question.

const PORT = 3000;

const express = require('express');
const fs = require('fs');
const csv = require('fast-csv');

const app = express();

app.listen(PORT, () => {
console.log(`Express server is listening on ${PORT}`);
});

app.get('/rates/csv', (req, res) => {

let ws = fs.createWriteStream(__dirname + '/tmp/rates.csv');

csv.write([
    ["a", "b"],
    ["a1", "b1"],
    ["a2", "b2"]
], {headers: true})
    .pipe(ws);

console.log('FILE SAVED');

res.download(ws.path, 'rates.csv')
});

1 个答案:

答案 0 :(得分:1)