Middleware for export data list to csv file.
If you have a api for getting data lists, but if you want to export these data lists to csv file, then only one thing you can do is to using this middleware package, but no need to refactor your api code.
$ npm install data2csv --save
First, you neend to set request headers: 'Accept' to 'text/csv', but not to 'application/json'.
and then add middleware
'use strict';
const data2csv = require('data2csv');
const double = (data, defaultValue) => {
return data && (data+data) || defaultValue;
};
// 'method' is option attribute
const fields = [
{ label: 'Code', value: 'code', method: double, default: '-' },
{ label: 'Name', value: 'name', default: '-' }
];
const handler = async (data, req, res) => {
// data = [{}, {}, ...];
// for ex. => data = [{ code: '123', name: 'name123' }, { code: '', name: 'name456' } ];
const csv = data2csv.convertJsonToCSV(data, fields);
// csv = 'Code,Name\n123123,name123\n-,name456\n'
// Code Name
// 123123 name123
// - name456
const filename = 'the export csv file name you want to call';
return { csv, filename };
};
module.exports = data2csv.mw(handler);
then add middleware to your app.js
file (before the route handling middleware)
app.use(require('./middleware.js'));
If you have any questions, please contact me root@laravue.org
The MIT license