Let us look at how to handle common use cases in a Node.js application:
10.1) Parsing CSV's using Node.js
In this post we will see how to create a very simple parser using CSV node module.
a) Creating the Express Project:
Create a quick express project in your workspace by hitting "express Parsing_CSV" from the cmd prompt. If this is your first time on express and need more details to get your environment set with express, please read my previous post here.
b) Adding the parser code
In the index.js under the router, add the following code:
Summary:
1) We are including the CSV node module.
2) In the command prompt add this module using "npm install csv"
3) Provide the input file to parse and the output file that is going to store the parsed file.
4) Rest of the code is straight forward.
c) Create the files
1) Under the route folder create a file called input.txt and add some lines.
2) Create another file called output.txt which will be initially blank.
d) Putting it all together
In the command prompt, traverse to under the project created and hit "node app.js". Launch the browser "http://localhost:3000".
Observe the output.txt which will have the input.txt contents parsed.
Alternate Flavors:
If you are looking to do the same with XLSX you can employ the use of XLSX module.
a) You can stream the file using XLSX.readFile("fileName.xlsx");
b) sheetName_List = xlsx.SheetNames
c) sheet1 = xlsx.Sheets[0];
d) JSON.stringify(sheet1) will show the contents as well.
10.1) Parsing CSV's using Node.js
In this post we will see how to create a very simple parser using CSV node module.
a) Creating the Express Project:
Create a quick express project in your workspace by hitting "express Parsing_CSV" from the cmd prompt. If this is your first time on express and need more details to get your environment set with express, please read my previous post here.
b) Adding the parser code
In the index.js under the router, add the following code:
|
/*
* GET home
page.
*/
exports.index = function(req,
res){
parseTheCSV();
res.render('index', { title: 'CSV PARSER' });
};
var csv = require('csv');
function parseTheCSV(){
csv().from.path(__dirname+'/input.txt').to.path(__dirname+'/output.txt').transform(function(data){
data.unshift(data.pop());
return
data;
})
.on('data',function(data,index){
console.log('Index is ............ '+index+'
'+JSON.stringify(data));
})
.on('end',function(count){
console.log('Number of lines from the
inout file: '+count);
})
.on('error',function(error){
console.log("error occured
............"+error.message);
});
}
|
Summary:
1) We are including the CSV node module.
2) In the command prompt add this module using "npm install csv"
3) Provide the input file to parse and the output file that is going to store the parsed file.
4) Rest of the code is straight forward.
c) Create the files
1) Under the route folder create a file called input.txt and add some lines.
2) Create another file called output.txt which will be initially blank.
d) Putting it all together
In the command prompt, traverse to under the project created and hit "node app.js". Launch the browser "http://localhost:3000".
Observe the output.txt which will have the input.txt contents parsed.
Alternate Flavors:
If you are looking to do the same with XLSX you can employ the use of XLSX module.
a) You can stream the file using XLSX.readFile("fileName.xlsx");
b) sheetName_List = xlsx.SheetNames
c) sheet1 = xlsx.Sheets[0];
d) JSON.stringify(sheet1) will show the contents as well.
Userful & Great Article for NodeJS Development
ReplyDeleteNode js Course
Nodejs Training