Read a CSV file using Node.js

Posted by: Mahesh Sabnis , on 10/23/2015, in Category Node.js
Views: 26056
Abstract: Use Node.js CSV module to easily read data stored on the server side and make it available to the client on request.

Several formats can be used for persisting data. If the data is not relational in nature, we can store it in flat files. Flat files was one of the most preferred form of data persistence in legacy apps, and continues to be in many modern apps as well. Comma Separated Files (CSV) is one such medium where data can be stored and used later.

Node.js provides CSV module using which we can Read/Write from and to CSV files. In this article, we will read a CSV file and expose the data as JSON so that any client application e.g. jQuery, Angular, DataTableJs, etc. can consume it.

 

Node.js CSV Reader Implementation

You can use any IDE of your choice like Sublime Text or the free Visual Studio Code. If you are using Visual Studio Code, you also need NTVS (Node Tools for Visual Studio). Here’s an entire article on the same Node Tools for Visual Studio.

Note: This application is developed using Visual Studio Code. This is a new editor by Microsoft for building and debugging modern web and cloud applications.

Step 1: To develop our application, we will create a folder structure which we will later open in Visual Studio Code. Create a new folder of name VSCodeReadCSVFile on your hard drive. Add a new subfolder datafile inside VSCodeWriteCSVFile. In the datafile folder, add a new file and call it datainfi.csv. Add the following data in it:

101,MS,100000
102,LS,80000
103,TS,60000
104,VB,200000
105,PB,180000
106,AB,160000

Step 2: Open Visual Studio Code and open the VSCodeReadCSVFile folder as shown in the following image:

vscode-openfolder

Step 3: In this folder, add a Scripts folder. We will use this to create an application JavaScript file.

scripts-folder

Step 4: In the Scripts folder add a new file of name app.js as shown in the following image

appjs

Step 5: Navigate to the VSCodeReadCSVFile folder from the Node Command prompt and initialize the TypeScript Definition for Node.js intellisense as shown in the following image:

tsd query node --action install

node-install

 

Step 6: Install the CSV node module using the following command from the Node Command prompt

csv-node-module

Step 7: Open app.js from the scripts folder and add the following code in it:

//1.
var csv = require('csv');
//2.
var obj = csv();
//3.
function Employee(eno, ename, sal) {

    this.EmpNo = eno;
    this.EmpName = ename;
    this.Salary = sal;
};

//4.
var Employees = [];

//5.
obj.from.path('../datafile/dataInfo.csv').to.array(function (data) {
    for (var index = 0; index < data.length; index++) {
        Employees.push(new Employee(data[index][0], data[index][1], data[index][2]));
    }
    console.log(Employees);
});
//6.
var http = require('http');
//7.
var server = http.createServer(function (req, resp) {
    resp.writeHead(200, { 'content-type': 'application/json' });
    resp.end(JSON.stringify(Employees));

});

//8.
server.listen(5050);

The above code does the following (The numbering matches with the Comment numbering in the above code.)

1. Loads the CSV module so that we can perform Read operation.

2. Get the CVS () function object to access required functionality.

3. Define the Employee object with parameterized constructor. We will be using this object for storing the data read from the CSV file into an array of Employee.

4. Employee array will contain the data from the CSV file, this will be sent to the clients request over http.

5. reads the CSV file from the path and the data is stored in an array using callback function. The function iterates through an array and each line record from the CSV file will be pushed as a record in an Employees array.

6. Load the http module.

7. Create a web server with request listener callback. This write the response header with the content type as json and also end the response by sending the Employees array in the JSON form.

8. The web server start listening the on the 5050 port.

Step 8: Right click on app.js. Select the option Open in Command Prompt. In this command prompt, enter the following command

Node app

This will show the following result

node-csv-result

Step 9: Open any browser and enter the following URL in address bar http://localhost:5050

..and you should see the following result

csv-browser

Now we can create a client application using jQuery, Angular, etc. and consume the data to show it on HTML page.

Conclusion

Using the Node.js CSV module, we can easily read data stored on the server side and make it available to the client on request.

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mahesh Sabnis is a DotNetCurry author and Microsoft MVP having over 17 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). Follow him on twitter @maheshdotnet


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook