var csv = require('csv')

function makeCSVfromJSON(data) {
  var rows = []
  var keys = Object.keys(data[0])
  rows.push(keys)
  data.forEach(function(obj){
    var row = []
    keys.forEach(function(key){
      row.push(obj[key])
    })
    rows.push(row)
  })
  return rows
}

function returnData(req, res, data) {
  if(req.query.csv) {
    res.contentType('csv')
    var rows = makeCSVfromJSON(data)
    var result = []
    csv()
    .from.array(rows)
    .to.string(function(output){
      res.set('Content-Type', 'text/csv')
      res.set('Content-disposition', 'attachment;filename=' + req.route.path.substring(1) + '.csv')
      res.send(output)
    })
  } else {
    res.json(data)
  }
}

module.exports = returnData
@@ -296,13 +296,8 @@
 ata(
-req, 
 res,
@@ -305,27 +305,28 @@
 data
-) %7B%0A  if(req.query.
+, csv, name) %7B%0A  if(
 csv)
@@ -585,35 +585,12 @@
 ' + 
-req.route.path.substring(1)
+name
  + '
@@ -682,16 +682,17 @@
 rts = returnData
+%0A

change return-data parameters