Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,22 @@ app.get('/headers', function(req, res) {
], true)
})

// Add custom name
app.get('/filename', function(req, res) {
res.csv([
{"a": 1, "b": 2, "c": 3},
{"a": 4, "b": 5, "c": 6}
], true, undefined, undefined, 'custom-file-name.csv')
})

// Add response headers and status code (will throw a 500 error code)
app.get('/all-params', function(req, res) {
res.csv([
{"a": 1, "b": 2, "c": 3},
{"a": 4, "b": 5, "c": 6}
], true, {
"Access-Control-Allow-Origin": "*"
}, 500)
}, 500, 'custom-name.csv')
})

app.listen(3000)
Expand Down
5 changes: 4 additions & 1 deletion lib/csv-express.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ function escape(field) {
{status} - Optional status code
*/

res.csv = function(data, csvHeaders, headers, status) {
res.csv = function(data, csvHeaders, headers, status, fileName) {
var body = '';
var headerRow = [];
var statusCodeSet = true

this.charset = this.charset || 'utf-8';
this.header('Content-Type', 'text/csv');
if (fileName) {
this.header('Content-disposition', `attachment; filename=${fileName}`)
}

// Set custom headers
if (headers && headers instanceof Object) {
Expand Down