Skip to content

Instantly share code, notes, and snippets.

@johnelliott
Created June 19, 2014 00:30
Show Gist options
  • Save johnelliott/8dac3894f9bed9f66037 to your computer and use it in GitHub Desktop.
Save johnelliott/8dac3894f9bed9f66037 to your computer and use it in GitHub Desktop.
Node File System (fs)
// File I/O is provided by simple wrappers around standard POSIX functions. To use this module do require('fs'). All the methods have asynchronous and synchronous forms.
// Watch for changes on filename. The callback listener will be called each time the file is accessed.
fs.watch('myfile.txt', function (event, filename) {
// fs.readFile asynchronously reads the entire contents of a file.
fs.readFile(file, {encoding:'utf8'}, function ( err, data ) {
if (err) throw err;
console.log(data);
});
});
// write out our file
// . . . and see what changes
fs.writeFile(file, "Hello NodeDC!");
setTimeout( function() {
fs.writeFile(file, "Hello NodeDC!");
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment