I would to write a simple NodeJS app, that reads a file, analyzes its lines one-by-one and writes the result. Here is the code sample that works
var lines;
var fs = require('fs');
var data="";
fs.readFile('list.csv', 'ascii', function(err,data){
if(err) {
console.error("Could not open file: %s", err);
process.exit(1);
}
var data2=data.split(/[\n\r]+/);
for(var i=0; i<data2.length; i++){
/*LISTING ALL THE LIST LINE-BY-LINE */
console.log(i + data2[i]);
}
});
I'd like to know why I should write my code inside function(err,data){..*my-code*..}
? I tried to declare all the variables as global and write
console.log();
at the end of the code – it seems it just dosen't execute this code line. So, why cant I write my code outside function(err,data){}
?