Skip to content

Instantly share code, notes, and snippets.

@neilff
Created December 19, 2013 17:22
Show Gist options
  • Save neilff/8042947 to your computer and use it in GitHub Desktop.
Save neilff/8042947 to your computer and use it in GitHub Desktop.
nodejs-read-write-example-async
var fs = require('fs');
var myData = {
name:'test',
version:'1.0'
},
outputFilename = 'my.json';
// Write cache file
fs.writeFile(outputFilename, JSON.stringify(myData, null, 4), function(err) {
if (err) {
console.log('Error: ' + err);
} else {
console.log('Output: ' + outputFilename);
}
});
// Read cache file
fs.readFile(outputFilename, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
}
data = JSON.parse(data);
console.dir(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment