Last active
August 29, 2015 13:56
-
-
Save sgimeno/9329706 to your computer and use it in GitHub Desktop.
Async and sync ways to read files with FS in node.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var file = __dirname + '/test.json'; | |
//Async read file | |
fs.readFile(file, 'utf8', function (err, data) { | |
if (err) { | |
console.log('Error: ' + err); | |
return; | |
} | |
var data = JSON.parse(data); | |
console.dir(data); | |
}); | |
//Sync alternative | |
var file = fs.readFileSync(__dirname + '/test.json', 'utf8'); | |
var data = JSON.parse(file); | |
console.dir(data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment