In the file index.js
within the directory 01-read-file
, develop a script that outputs the contents of the file text.txt
to the console.
- The use of any third-party modules is prohibited.
- Each task must be executed in the root directory using the command
node <task folder name>
. - The use of synchronous functions from the fs module, such as
fs.statSync(path[, options])
,fs.readFileSync(path[, options])
, and others found in the synchronous API section, is prohibited.
- Inside the folder
01-read-file
, there are 2 files,index.js
andtext.txt
. - When executing the command
node 01-read-file
in the root directory of the repository, the contents of the filetext.txt
should be printed to the console. - Synchronous file reading methods should not be used in the code.
- File reading should occur using ReadStream.
- Get acquainted with the basics of working with the file system on the Node.js platform.
- Learn the basics of streams and events.
- Familiarize yourself with the Path module and learn to use it to construct absolute paths to files.
In this task, you are required to develop a small script that outputs the contents of a pre-prepared text file to the console. You can follow these steps:
- Import the necessary modules for task execution:
- To interact with the file system in Node.js, use the fs module.
- For correctly specifying the file path, you will need the Path module.
- Create a new ReadStream from the file
text.txt
. - Direct the read stream to the standard output stream.
For imports in Node.js, use CommonJS modules.
Despite the fact that Node.js now has almost full support for ECMAScript modules (import/export
), this approach is not yet fully stable, and the vast majority of code you encounter will be written using CommonJS:
const fs = require('fs');
For reading the file, you will use streams, which are an important and useful part of the platform. Thanks to them, you can process large amounts of data chunk by chunk on the fly, while consuming a minimal amount of resources, instead of loading them into memory entirely. In your future work, you will encounter them repeatedly.
It's crucial to familiarize yourself with another fundamental concept in Node.js: Events. Node.js extensively uses events, and most objects are instances of the EventEmitter
class. Understanding streams' operation is greatly enhanced by first getting to know events, as every stream is a descendant of EventEmitter
.
Materials on these topics are also attached in the Useful Links section.
When creating a ReadStream, pay attention to the fact that the command to run your code should be executed in the root directory of the repository. Therefore, it is important to correctly pass the file path for reading.
Node.js, in the case of passing a relative path to the file like ./text.txt
, will look for it relative to the directory in which the process was started.
The join function from the Path module allows you to create a full path to the text file based on the variable __dirname
, which stores the path to the directory where your script file is located. Thus, the directory from which you run the code will not affect the location of the required file, and you will always refer to text.txt
located next to index.js
.
The Path module also contains other useful functions for manipulating paths, so I strongly recommend studying its capabilities.
You will have several options for directing your read stream to the standard output stream (i.e., the console). You can use both the high-level console.log() and work directly with the output stream process.stdout.
If you're looking for the information in Russian, please note that translations of documentation into Russian may be outdated and may not contain all the modern features of the modules.
For up-to-date information, always use the official documentation!