Nodejs write file method basics and more
The nodejs read file file system method is a method in node build in file system module. This method might work just fine when I just want to read a file in full, and not do anything fancy with streaming or reading by way of a buffer. In most cases this method will work fine if I just simple want to read a small file, however it is not a golden hammer for all situations in which I need to read data from the local file system. Never the less it would seem that I never got around to writing a post on this method, so lets get this one out of the way.
1 - Some basic examples of the nodejs read file method
In this section I will be starting out with some very basic examples of the read file method. I also use this kind of section to bring up other things that should be known before continuing to read the rest of the post. One thing I often bring up is that this is not a getting started type post with nodejs, or javaScript in general, so I assume you have at least some background with this topics.
1.1 - The source code examples here are up on guthub
As I come around to editing my old nodejs posts, I have made it a habit to make sure I always link to any github project in which I have the source code for the examples that I am writing about in the post. With that said the source code examples here can be found in my core nodejs demos github repository.
1.2 - very basic hello world style example of fs.readFile
First off with examples here I have a very basic example of the read file method where I am using the read file method to read the source code file itself. With that said I named this script basic.js and when I call node and pass the basic.js name of the file the result will be the source code of the file itself spit out to the standard output of the console.
|
|
If I do not give an encoding when calling the read file method, the result that will be returned is a buffer for the data. This is because the default encoding for the read file method is binary. So then when it comes to reading text files there is the option of giving an encoding value when calling the method, or using the to string method of the buffer instance like I did in this example.
1.3 - Setting the encoding
Often I am working with text files rather than some kind of binary data. Although I can always just call the to string method of a buffer, often I give the utf8 encoding in order to read text files.
|
|
1.4 - Error handing
The first argument of the callback function used with the read file method is for any errors that might happen when reading a file. If all goes well the value of this error argument is null when resolves to a false value. However when something does go bad there are a few things that can happen when reading files. The most typical errors though would be not file access permissions, and the file not being there.
|
|
2 - return a promise on old version sof node
In late version of nodejs a promise will be returned when using the read file method, however this was not always the case. If for some reason I want to support older version of nodejs I will want to use one or more ways to make a new method that will return a promise that makes use of the read file method. ways to go about doing this sort of thing would include using some kind of feature in core javaScriot, nodejs, or a user space library.
2.1 - Using the promisify method of the util module
In this example I am using the promisify method of the nodejs build in util module.
|
|
3 - Conclusion
That will be it for now when it comes to the read file method of the file system module in nodejs. For now I just wanted to cover the basics of the method at least, however I do have plans to further expand this post when I get some more time to do so.