Last active
April 4, 2018 13:27
-
-
Save abhiche/f71e6d6c3706392a080146e8ad006e28 to your computer and use it in GitHub Desktop.
Using streams to parse and transform CSV - NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const csv = require('csv'); | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
app.use(bodyParser.json()); | |
app.get('/parse', (req, res) => { | |
res.set('Content-Type', 'application/json'); // Set it if you are sending json | |
csv | |
.generate({seed: 1, columns: 2, length: 20}) // generates a random csv for testing, instead use your read stream for your purpose | |
.pipe(csv.parse()) // parses data from the stream | |
.pipe(csv.transform(function(record){ | |
return JSON.stringify({ // stringify is required to pass to the next pipe since it doesn't understand json | |
col1: record[0], | |
col2: record[1] | |
}); | |
})) | |
.pipe(res); | |
}) | |
app.listen(8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment