Friday, April 5, 2013

Parsing uploaded CSV file using node.js, Express

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.


6 comments:

  1. Thank your valuable content.we are very thankful to you.one of the recommended blog.which is very useful to new learners and professionals.content is very useful for hadoop learners


    Best Spring Online Training Institute
    Best Devops Online Training Institute
    Best Datascience Online Training Institute

    ReplyDelete

  2. I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting!!
    Best nodejs Training Institute

    ReplyDelete
  3. Aol Helpline Number Aol technical support Helpline number assist you in technical questions while
    using aol email services And facing any issues related to Aol login,emails issues, are answered
    by Aol Helpline team.Please contact us at +1800-284-6979. our customer support officer are available 24x7 round o clock.feel free to contact us.
    Aol Helpline Number

    ReplyDelete
  4. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this. This is good information and really helpful for the people who need information about this.

    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    oracle online training

    hadoop training in chennai

    hadoop training in bangalore


    ReplyDelete

Followers

About Me

I'm a software developer with interests in Design Patterns, Distributed programming, Big Data, Machine Learning and anything which excites me. I like to prototype new ideas and always on the lookout for tools which help me get the job done faster. Currently, i'm loving node.js + Mongodb.