如何使用Node.js的fs模块实现文件下载并表达

Whi*_*hip 8 download node.js express

因为文件应该是动态生成的,所以我应该使用fs模块的writeStream.但我找不到任何与我糟糕的谷歌搜索的示例代码.抱歉.

更具体地说,当有人请求时,我想在MongoDB中提供带有我的数据的CSV文件或PDF文件.

任何人,请给我一些提示.

谢谢.

Whi*_*hip 16

有了快递,我可以这样实现.

app.get('/down2', function(req, res){
  var filename = 'data.csv';
  res.attachment(filename);
  res.end('hello,world\nkeesun,hi', 'UTF-8'); //Actually, the data will be loaded form db.
});
Run Code Online (Sandbox Code Playgroud)

它有多简单.谢谢.