diff --git a/lib/express-csv.js b/lib/express-csv.js index fcc1d69..9e75d31 100644 --- a/lib/express-csv.js +++ b/lib/express-csv.js @@ -98,7 +98,13 @@ res.csv = function(obj, headers, status) { this.charset = this.charset || 'utf-8'; this.header('Content-Type', 'text/csv'); - obj.forEach(function(item) { + obj.forEach(function(item,i) { + if (i == 0 && item instanceof Object) { + keys = Object.keys(item); + if (typeof keys !== "undefined" && keys instanceof Array && keys.length > 0) { + body += keys.map(escape).join(exports.separator) + '\r\n'; + } + } if (!(item instanceof Array)) item = objToArray(item); body += item.map(escape).join(exports.separator) + '\r\n'; });