res.download() not working in my case

Let Express set the correct headers and just do this:

res.get('/download', function(req, res) {
  res.download(__dirname + 'jsonFile.json', 'jsonFile.json');
});

(doc)

EDIT: since you’re requesting /download through an AJAX call, you have to change your setup because most (all?) browsers will not show a download dialog in that case.

Instead, you can create a new window from your front end code to trigger the dialog:

window.open('/download?foo=bar&xxx=yyy');

Leave a Comment