This afternoon i was looking for a CSV parser in node.js. This was the easiest one to use.
https://github.com/koles/ya-csv
Following is the code for parsing the CSV file. I'm using Express to build a Web app. If you would like to read more about Express go here
http://expressjs.com/
First, install the parser using the following command:
npm install ya-csv
npm is used for dependency management for node.js.
My sample CSV file looks like so :
Kukku, Nambiar, kukku@kukku.com, +01123457666
Kukku, Nambiar, kukku@kukku.com, +01123457666
Kukku, Nambiar, kukku@kukku.com, +01123457666
Kukku, Nambiar, kukku@kukku.com, +01123457666
Kukku, Nambiar, kukku@kukku.com, +01123457666
Kukku, Nambiar, kukku@kukku.com, +01123457666
Kukku, Nambiar, kukku@kukku.com, +01123457666
Kukku, Nambiar, kukku@kukku.com, +01123457666
Kukku, Nambiar, kukku@kukku.com, +01123457666
Kukku, Nambiar, kukku@kukku.com, +01123457666
Following is my html code used for the file upload:
<form action="/upload/group" method="POST" enctype="multipart/form-data">
<input type="file" name="groupfile">
<button type="submit" class="btn">Submit</button>
</form>
All the files uploaded (in this case only one called groupfile) is available in the HTTP req variable
called req in this example.
var csv = require('ya-csv');
app.post('/upload/group', function(req, res) {
console.log('File name is ' + req.files.groupfile.name);
console.log('File size is ' + req.files.groupfile.size);
console.log('File size is ' + req.files.groupfile.path);
var reader = csv.createCsvFileReader(req.files.groupfile.path, {
'separator': ',',
'quote': '"',
'escape': '"',
'comment': ''
});
reader.addListener('data', function(data) {
console.log(data);
});
});
Following is the output when i upload a file called test.csv
File name is test.csv
File size is 461
File size is /tmp/4289570db2af84a242ea7f995a3aa01a
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
[ 'Kukku', ' Nambiar', ' kukku@kukku.com', ' +01123457666' ]
The above code creates a CSV reader using ya-csv. Note that req.files.groupfile.path points to the path of the uploaded file on the disk. It is saved automatically and you do not have to save it yourself.
var reader = csv.createCsvFileReader(req.files.groupfile.path, {
'separator': ',',
'quote': '"',
'escape': '"',
'comment': ''
});
Thast's it, when a record (a line) is read the 'data' listener is triggered and you get each row from the csv file. You can hook up the 'end' event listener as well to detect the end of the file like so:
reader.addListener('end', function() {
console.log('Closed event received now');
});
You would need to hookup the 'end' event listener to render a view to the user like below:
reader.addListener('end', function() {
res.render('success_csv_upload', {message : "Successfully consumed the CSV file");
});
Otherwise, the page would just keep spinning there waiting for a response. Make sure you have a view with that name otherwise this call will fail. The point i'm trying to make is that the 'end' event listener signals the end of CSV file parsing and time to get back to the user.
This is a paradigm shift in the way we write code if you are not coming from a event driven style of programming background. But i'm sure you will get used to it in a while playing with node. I find node to be a great framework for web applications. I haven't explored it extensively, but i think it has a promising future.
I will be publishing this source on github so that you can clone the entire web application which demonstrates the file upload and CSV parsing.