node append file fs method
In nodejs projects file system management comes up a lot, and so does the act of appending a file that all ready exists rather than completely rewriting a file all together. The node append file fs module method is a file system method that can be used to quickly finish most tasks that have to do with creating a file if it does not exist, or just simply appending to the end of it if it does. A task that is common of most log files that have to do with just simply spitting out a recode or what is going on with something.
1 - node append file method basic example
A basic example of appending a file in node js might involve just using the append file file system module method to do so. this means just using the built in nodejs file system module, and then use thee fs.appendFile method passing the path to the file to be appended, followed by the text of buffer data to append the file with, followed by a call back that will fire when the appending has completed.
|
|
2 - The node append file method and the EMFILE Error
In some situations the appendFile method might work out okay, but there is one little thing at least to be aware of and that is the EMFILE error that can happen when there are to many open files. So in the event that this becomes a problem it might be best to take advantage of one of the alternatives to that of the append file method such as fs.createWriteStream.
For example the following will result in an EMFILE Error
|
|
Using fs.createWriteSTream will solve this.
|
|