Let's start learning Node.js by printing "Hello World" in the console, just like when we started learning other programming and scripting languages.
To print "Hello World" in the console, type the simple command:
console.log("Hello World!");
We can run the file in the command prompt by giving the command:
node <filename with extension as js>
For example, if the filename is HelloFirst, then the command should be like: node HelloFirst.js
How to Read from a File and Write to a File
Firstly, we have to import the fs module in order to do the reading, and writing to a file. We can do so by using the require command, and storing the returned value in a variable. For example:
const fs=require('fs');
Keyword "const" makes the variable final, that is, the value stored in such a variable cannot be changed later.
Since we need a file to read from, we can create a text file in the same folder which contains our Node.js file with a sentence in it. For example, we can create a text file named firstfile.txt and add a sentence like "I love coding" and save it. Later on, we will be reading this file using Node,js.
After creating the file, we have to give the location of the file which the reading is supposed to be done from. The system variable __dirname is used to get the directory name of the directory in which the Node.js file is, and using this, we can give the location of the file. We can use this variable since the file we are intending to read from is also located in the same folder as the Node.js file. The following code achieves this part:
const readFromFile = __dirname + '/firstfile.txt';
We also have to give the location where the new file to which we are writing whatever read from the other file is to be stored. We can make use of the variable __dirname for this too if we are planning to save the new file in the same folder as the other two files(Node.js file and the text file). This particular part is achieved by the code below:
const writeToFile = __dirname + '/copiedfile.txt';
As the next step, we create a read stream-which allows you to read data from a source-to the file we are reading from, which is the source file:
const streamForReading = fs.createReadStream(readFromFile);
Since we need a stream to use in order to write whatever read, we will create a write stream-which allows you to write data to a destination-to the file we are writing to, which is the destination file:
const streamForWriting = fs.createWriteStream(writeToFile);
As the final step, we will pipe the read stream to the write stream. Piping simply means that we are passing or feeding the output of one method (reading in this case) as the input to another method (writing in this case), so it is like creating a pipe connection between two methods where the end of one pipe connects to the beginning of another pipe. The code for pipe-lining:
streamForReading.pipe(streamForWriting);
If we go and check the folder with the Node.js file and the text file now, we'll be able to find the new file (named copiedfile.txt in this case) created there with the same content as the original text file we had.
const readFromFile = __dirname + '/firstfile.txt';
We also have to give the location where the new file to which we are writing whatever read from the other file is to be stored. We can make use of the variable __dirname for this too if we are planning to save the new file in the same folder as the other two files(Node.js file and the text file). This particular part is achieved by the code below:
const writeToFile = __dirname + '/copiedfile.txt';
As the next step, we create a read stream-which allows you to read data from a source-to the file we are reading from, which is the source file:
const streamForReading = fs.createReadStream(readFromFile);
Since we need a stream to use in order to write whatever read, we will create a write stream-which allows you to write data to a destination-to the file we are writing to, which is the destination file:
const streamForWriting = fs.createWriteStream(writeToFile);
As the final step, we will pipe the read stream to the write stream. Piping simply means that we are passing or feeding the output of one method (reading in this case) as the input to another method (writing in this case), so it is like creating a pipe connection between two methods where the end of one pipe connects to the beginning of another pipe. The code for pipe-lining:
streamForReading.pipe(streamForWriting);
If we go and check the folder with the Node.js file and the text file now, we'll be able to find the new file (named copiedfile.txt in this case) created there with the same content as the original text file we had.