FileReader: this object exposes a set of methods that allows the client to read from a file. Reading happens asynchronously, and triggers a FileReader.onload event when it has completed. Any methods you want to interact with the contents of a file that has been read should therefore be included in the
FileReader is a webAPI, and was not automatically accessible in my node JS runtime. I installed two NPM packages that implemented the APIs:
file-api. These allowed me to create both FileReader objects and File objects (which the reader expects as input to its
See below for the implementation I used to read from text files in my codeJam solutions.
I create an instance of the FileReader object called
reader, then attach a callback to the reader’s
onLoad() event which will fire asynchronously whenever it finishes reading input from a file.
I use the
reader.readAsText() method to read in the file, which attaches a string representation of the textfile to ‘result’ when it has completed reading.
readAsText() expects a
File object, which was implemented by the
file-api package I installed.
NOTE: Upon further examination of the types of objects that both of the above NPM packages implement, it looks like I could have gotten away with just requiring
file-api, since it also implements FileReader. It also looks like it might be possible to write to text files on the client according to this SO post.