From the course: Node.js Essential Training

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Reading files

Reading files - Node.js Tutorial

From the course: Node.js Essential Training

Start my 1-month free trial

Reading files

- [Instructor] Reading files is another feature of our FS module. It's going to allow us to read the contents of a file, encoded into some sort of a text format. What we'll do first is we want to continue to import our File System module. Then in order to check this out, we want to take a look over here in our start folder. And notice that we have this file called readme.md. It's filled with some Lorem ipsum text. We just need to read something, so might as well read some nonsense there. What we'll do here is we want to go ahead and replace the code that we had with a variable called ipsum. Set that equal to readFileSync. We're going to read this synchronously. And I'm going to include the name of the file, readme.md, as well as the text encoding format for the file, which is in most cases for text files going to be UTF-8. Then we'll go ahead and log this to our console. We should be able to see the contents from our…

Contents