Last active
June 9, 2017 20:45
-
-
Save igoralves1/7481521ff3e0b68baf0ad39fc9b021fd to your computer and use it in GitHub Desktop.
NodeJs
This file contains hidden or 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
| //http://thisdavej.com/beginners-guide-to-installing-node-js-on-a-raspberry-pi/ | |
| //http://weworkweplay.com/play/raspberry-pi-nodejs/ | |
| //https://learn.adafruit.com/node-embedded-development/installing-node-dot-js | |
| //Reading from a Stream | |
| /* | |
| var fs = require("fs"); | |
| var data = ''; | |
| // Create a readable stream | |
| var readerStream = fs.createReadStream('input.txt'); | |
| // Set the encoding to be utf8. | |
| readerStream.setEncoding('UTF8'); | |
| // Handle stream events --> data, end, and error | |
| readerStream.on('data', function(chunk) { | |
| data += chunk; | |
| }); | |
| readerStream.on('end',function(){ | |
| console.log(data); | |
| }); | |
| readerStream.on('error', function(err){ | |
| console.log(err.stack); | |
| }); | |
| console.log("Program Ended"); | |
| */ | |
| //////////////////////////////////////////////////////////////////////////////// | |
| //Writing to a Stream | |
| /* | |
| var fs = require("fs"); | |
| var data = 'Simply Easy Learning'; | |
| // Create a writable stream | |
| var writerStream = fs.createWriteStream('output.txt'); | |
| // Write the data to stream with encoding to be utf8 | |
| writerStream.write(data,'UTF8'); | |
| // Mark the end of file | |
| writerStream.end(); | |
| // Handle stream events --> finish, and error | |
| writerStream.on('finish', function() { | |
| console.log("Write completed."); | |
| }); | |
| writerStream.on('error', function(err){ | |
| console.log(err.stack); | |
| }); | |
| console.log("Program Ended"); | |
| */ | |
| //////////////////////////////////////////////////////////////////////////////// | |
| /* | |
| //Piping is a mechanism where we provide the output of one stream as the input to | |
| //another stream. It is normally used to get data from one stream and to pass the | |
| //output of that stream to another stream. There is no limit on piping operations. | |
| //Now we'll show a piping example for reading from one file and writing it | |
| //to another file. | |
| var fs = require("fs"); | |
| // Create a readable stream | |
| var readerStream = fs.createReadStream('input.txt'); | |
| // Create a writable stream | |
| var writerStream = fs.createWriteStream('output.txt'); | |
| // Pipe the read and write operations | |
| // read input.txt and write data to output.txt | |
| readerStream.pipe(writerStream); | |
| console.log("Program Ended"); | |
| */ | |
| //////////////////////////////////////////////////////////////////////////////// | |
| //Chaining the Streams | |
| /* | |
| //Chaining is a mechanism to connect the output of one stream to another stream | |
| //and create a chain of multiple stream operations. It is normally used with | |
| //piping operations. Now we'll use piping and chaining to first compress a file | |
| //and then decompress the same. | |
| var fs = require("fs"); | |
| var zlib = require('zlib'); | |
| //Compress the file input.txt to input.txt.gz | |
| fs.createReadStream('input.txt') | |
| .pipe(zlib.createGzip()) | |
| .pipe(fs.createWriteStream('input.txt.gz')); | |
| console.log("File Compressed."); | |
| var fs = require("fs"); | |
| var zlib = require('zlib'); | |
| //Decompress the file input.txt.gz to input.txt | |
| fs.createReadStream('input.txt.gz') | |
| .pipe(zlib.createGunzip()) | |
| .pipe(fs.createWriteStream('input.txt')); | |
| console.log("File Decompressed."); | |
| */ | |
| //////////////////////////////////////////////////////////////////////////////// | |
| //Node.js - File System | |
| /* | |
| //Node implements File I/O using simple wrappers around standard POSIX functions. | |
| //The Node File System (fs) module can be imported using the following syntax − | |
| //var fs = require("fs"); | |
| //Synchronous vs Asynchronous | |
| //Every method in the fs module has synchronous as well as asynchronous forms. | |
| //Asynchronous methods take the last parameter as the completion function callback | |
| //and the first parameter of the callback function as error. It is better to use | |
| //an asynchronous method instead of a synchronous method, as the former never | |
| //blocks a program during its execution, whereas the second one does. | |
| //Example | |
| //Create a text file named input.txt with the following content − | |
| var fs = require("fs"); | |
| // Asynchronous read | |
| fs.readFile('input.txt', function (err, data) { | |
| if (err) { | |
| return console.error(err); | |
| } | |
| console.log("Asynchronous read: " + data.toString()); | |
| }); | |
| // Synchronous read | |
| var data = fs.readFileSync('input.txt'); | |
| console.log("Synchronous read: " + data.toString()); | |
| //Asynchronous X Synchronous | |
| console.log("Program Ended"); | |
| */ | |
| //////////////////////////////////////////////////////////////////////////////// | |
| //Open a File | |
| /* | |
| Syntax | |
| Following is the syntax of the method to open a file in asynchronous mode − | |
| fs.open(path, flags[, mode], callback) | |
| Parameters | |
| Here is the description of the parameters used − | |
| path − This is the string having file name including path. | |
| flags − Flags indicate the behavior of the file to be opened. All possible values have been mentioned below. | |
| mode − It sets the file mode (permission and sticky bits), but only if the file was created. It defaults to 0666, readable and writeable. | |
| callback − This is the callback function which gets two arguments (err, fd). | |
| Flags | |
| Flags for read/write operations are − | |
| Flag Description | |
| r Open file for reading. An exception occurs if the file does not exist. | |
| r+ Open file for reading and writing. An exception occurs if the file does not exist. | |
| rs Open file for reading in synchronous mode. | |
| rs+ Open file for reading and writing, asking the OS to open it synchronously. See notes for 'rs' about using this with caution. | |
| w Open file for writing. The file is created (if it does not exist) or truncated (if it exists). | |
| wx Like 'w' but fails if the path exists. | |
| w+ Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists). | |
| wx+ Like 'w+' but fails if path exists. | |
| a Open file for appending. The file is created if it does not exist. | |
| ax Like 'a' but fails if the path exists. | |
| a+ Open file for reading and appending. The file is created if it does not exist. | |
| ax+ Like 'a+' but fails if the the path exists. | |
| */ | |
| //var fs = require("fs"); | |
| // | |
| //// Asynchronous - Opening File | |
| //console.log("Going to open file!"); | |
| //fs.open('input.txt', 'r+', function(err, fd) { | |
| // if (err) { | |
| // return console.error(err); | |
| // } | |
| // console.log("File opened successfully!"); | |
| //}); | |
| //Get File Information | |
| /* | |
| Syntax | |
| Following is the syntax of the method to get the information about a file − | |
| fs.stat(path, callback) | |
| Parameters | |
| Here is the description of the parameters used − | |
| path − This is the string having file name including path. | |
| callback − This is the callback function which gets two arguments (err, stats) | |
| where stats is an object of fs.Stats type which is printed below in the example. | |
| Apart from the important attributes which are printed below in the example, | |
| there are several useful methods available in fs.Stats class which can be used | |
| to check file type. These methods are given in the following table. | |
| Method Description | |
| stats.isFile() Returns true if file type of a simple file. | |
| stats.isDirectory() Returns true if file type of a directory. | |
| stats.isBlockDevice() Returns true if file type of a block device. | |
| stats.isCharacterDevice() Returns true if file type of a character device. | |
| stats.isSymbolicLink() Returns true if file type of a symbolic link. | |
| stats.isFIFO() Returns true if file type of a FIFO. | |
| stats.isSocket() Returns true if file type of asocket. | |
| */ | |
| //var fs = require("fs"); | |
| // | |
| //console.log("Going to get file info!"); | |
| //fs.stat('input.txt', function (err, stats) { | |
| // if (err) { | |
| // return console.error(err); | |
| // } | |
| // console.log(stats); | |
| // console.log("Got file info successfully!"); | |
| // | |
| // // Check file type | |
| // console.log("isFile ? " + stats.isFile()); | |
| // console.log("isDirectory ? " + stats.isDirectory()); | |
| //}); | |
| //////////////////////////////////////////////////////////////////////////////// | |
| //Writing a File | |
| /* | |
| Syntax | |
| Following is the syntax of one of the methods to write into a file − | |
| fs.writeFile(filename, data[, options], callback) | |
| This method will over-write the file if the file already exists. If you want to | |
| write into an existing file then you should use another method available. | |
| Parameters | |
| Here is the description of the parameters used − | |
| path − This is the string having the file name including path. | |
| data − This is the String or Buffer to be written into the file. | |
| options − The third parameter is an object which will hold {encoding, mode, | |
| flag}. By default. encoding is utf8, mode is octal value 0666. and flag is 'w' | |
| callback − This is the callback function which gets a single parameter err that | |
| returns an error in case of any writing error. | |
| */ | |
| //var fs = require("fs"); | |
| // | |
| //console.log("Going to write into existing file"); | |
| //fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) { | |
| // if (err) { | |
| // return console.error(err); | |
| // } | |
| // | |
| // console.log("Data written successfully!"); | |
| // console.log("Let's read newly written data"); | |
| // fs.readFile('input.txt', function (err, data) { | |
| // if (err) { | |
| // return console.error(err); | |
| // } | |
| // console.log("Asynchronous read: " + data.toString()); | |
| // }); | |
| //}); | |
| //////////////////////////////////////////////////////////////////////////////// | |
| //Reading a File | |
| /* | |
| Syntax | |
| Following is the syntax of one of the methods to read from a file − | |
| fs.read(fd, buffer, offset, length, position, callback) | |
| This method will use file descriptor to read the file. If you want to read the | |
| file directly using the file name, then you should use another method available. | |
| Parameters | |
| Here is the description of the parameters used − | |
| fd − This is the file descriptor returned by fs.open(). | |
| buffer − This is the buffer that the data will be written to. //https://pt.wikipedia.org/wiki/Buffer_(ci%C3%AAncia_da_computa%C3%A7%C3%A3o) | |
| offset − This is the offset in the buffer to start writing at. | |
| length − This is an integer specifying the number of bytes to read. | |
| position − This is an integer specifying where to begin reading from in the | |
| file. If position is null, data will be read from the current file position. | |
| callback − This is the callback function which gets the three arguments, (err, | |
| bytesRead, buffer). | |
| */ | |
| //var fs = require("fs"); | |
| //var buf = new Buffer(1024); | |
| // | |
| //console.log("Going to open an existing file"); | |
| //fs.open('input.txt', 'r+', function(err, fd) { | |
| // if (err) { | |
| // return console.error(err); | |
| // } | |
| // console.log("File opened successfully!"); | |
| // console.log("Going to read the file"); | |
| // fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ | |
| // if (err){ | |
| // console.log(err); | |
| // } | |
| // console.log(bytes + " bytes read"); | |
| // | |
| // // Print only read bytes to avoid junk. | |
| // if(bytes > 0){ | |
| // console.log(buf.slice(0, bytes).toString()); | |
| // } | |
| // }); | |
| //}); | |
| //////////////////////////////////////////////////////////////////////////////// | |
| //Closing a File | |
| /* | |
| Syntax | |
| Following is the syntax to close an opened file − | |
| fs.close(fd, callback) | |
| Parameters | |
| Here is the description of the parameters used − | |
| fd − This is the file descriptor returned by file fs.open() method. | |
| callback − This is the callback function No arguments other than a possible | |
| exception are given to the completion callback. | |
| */ | |
| //var fs = require("fs"); | |
| //var buf = new Buffer(1024); | |
| // | |
| //console.log("Going to open an existing file"); | |
| //fs.open('input.txt', 'r+', function(err, fd) { | |
| // if (err) { | |
| // return console.error(err); | |
| // } | |
| // console.log("File opened successfully!"); | |
| // console.log("Going to read the file"); | |
| // | |
| // fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ | |
| // if (err){ | |
| // console.log(err); | |
| // } | |
| // | |
| // // Print only read bytes to avoid junk. | |
| // if(bytes > 0){ | |
| // console.log(buf.slice(0, bytes).toString()); | |
| // } | |
| // | |
| // // Close the opened file. | |
| // fs.close(fd, function(err){ | |
| // if (err){ | |
| // console.log(err); | |
| // } | |
| // console.log("File closed successfully."); | |
| // }); | |
| // }); | |
| //}); | |
| //////////////////////////////////////////////////////////////////////////////// | |
| //Truncate a File | |
| /* | |
| Syntax | |
| Following is the syntax of the method to truncate an opened file − | |
| fs.ftruncate(fd, len, callback) | |
| Parameters | |
| Here is the description of the parameters used − | |
| fd − This is the file descriptor returned by fs.open(). | |
| len − This is the length of the file after which the file will be truncated. | |
| callback − This is the callback function No arguments other than a possible | |
| exception are given to the completion callback. | |
| */ | |
| //var fs = require("fs"); | |
| //var buf = new Buffer(1024); | |
| // | |
| //console.log("Going to open an existing file"); | |
| //fs.open('input.txt', 'r+', function(err, fd) { | |
| // if (err) { | |
| // return console.error(err); | |
| // } | |
| // console.log("File opened successfully!"); | |
| // console.log("Going to truncate the file after 10 bytes"); | |
| // | |
| // // Truncate the opened file. | |
| // fs.ftruncate(fd, 10, function(err){ | |
| // if (err){ | |
| // console.log(err); | |
| // } | |
| // console.log("File truncated successfully."); | |
| // console.log("Going to read the same file"); | |
| // | |
| // fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ | |
| // if (err){ | |
| // console.log(err); | |
| // } | |
| // | |
| // // Print only read bytes to avoid junk. | |
| // if(bytes > 0){ | |
| // console.log(buf.slice(0, bytes).toString()); | |
| // } | |
| // | |
| // // Close the opened file. | |
| // fs.close(fd, function(err){ | |
| // if (err){ | |
| // console.log(err); | |
| // } | |
| // console.log("File closed successfully."); | |
| // }); | |
| // }); | |
| // }); | |
| //}); | |
| //////////////////////////////////////////////////////////////////////////////// | |
| //Delete a File | |
| /* | |
| Syntax | |
| Following is the syntax of the method to delete a file − | |
| fs.unlink(path, callback) | |
| Parameters | |
| Here is the description of the parameters used − | |
| path − This is the file name including path. | |
| callback − This is the callback function No arguments other than a possible | |
| exception are given to the completion callback. | |
| */ | |
| var fs = require("fs"); | |
| console.log("Going to delete an existing file"); | |
| fs.unlink('input.txt', function(err) { | |
| if (err) { | |
| return console.error(err); | |
| } | |
| console.log("File deleted successfully!"); | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment