Skip to content

Instantly share code, notes, and snippets.

@mediaupstream
Last active March 2, 2018 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mediaupstream/fe51d6f4b7ebbb4af56d6913f2ddfe1d to your computer and use it in GitHub Desktop.
Save mediaupstream/fe51d6f4b7ebbb4af56d6913f2ddfe1d to your computer and use it in GitHub Desktop.
Download csv in Node.js

Using express.js you can force a file download by setting the appropriate http headers and content type.

This example forces a .csv file download.

app.get('/csv', (req, res) => {
let data = "name,age,status\nderek,30,online\nsarah,28,offline\nDan,36,online"
let filename = 'cool.csv'
res.setHeader('Content-disposition', `attachment; filename=${filename}`)
res.set('Content-Type', 'text/csv')
res.send(data)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment