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

由于该文件应该dynamic生成,也许我应该使用fs模块的writeStream。 但我找不到任何示例代码与我可怜的谷歌search。 抱歉。

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

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

谢谢。

随着快递 ,我可以这样实施。

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. }); 

它有多简单 谢谢。

你不需要fs,只需要在请求处理程序中从数据库stream数据

您可以使用Content-Disposition标题设置文件元数据

 Content-Type: image/jpeg Content-Disposition: attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"; Content-Description: a complete map of the human genome