Skip to main content

Command Palette

Search for a command to run...

Day 2 of 100days of code.

Introduction to Node JS

Published
β€’3 min read
Day 2 of 100days of code.
P

πŸ‘‹ Hi there! I'm Promise, a passionate MERN Stack Software Developer with a knack for turning ideas into captivating digital experiences. I specialise in creating responsive and user-friendly interfaces that not only meet but exceed the expectations of clients.

Reading and writing into files synchronously

When we use JavaScript in the browser, we cannot read and write files but this is possible using Node JS which provides the API to read and write files. To implement this, We use the file system module, fs for short. const fs = require("fs") To import module. The fs variable has a function called readFileSync, fs.readFileSync(). This function reads the file synchronously. We also have a function on this fs module that can read files asynchronously.

  • Create a folder named files

  • Create an input.txt file inside the files folder and write some text.

Reading a file

To read the input from the input.txt file, we need to pass the path of that file to the file sync. We start with the parent directory and then target our files folder and then the input.txt file. Then the next parameter is the encoding: "utf-8" i.e fs.readFileSync("./Files/input.txt", "utf-8"). You can create a variable to store the content so we can log to the console. i.e const readText = fs.readFileSync("./Files/input.txt", "utf-8") then console.log(readText)πŸ‘‡πŸ‘‡πŸ‘‡

The above method reads the files synchronously, and we know that JavaScript is a single threaded programming language, it executes the JavaScript code line by line. This type of execution is referred to as synchronous execution. This method can actually affect the performance of the application because no matter how long it takes, we have to wait for each of code to execute before we move to the next. This is why in Node JS we also have the asynchronous method of execution. This method does not block the next line of code.

Writing into a file

We have another method for writing into files synchronously which is the writeFileSync().

  • Create an output.txt file inside the files folder and leave it empty and blank. πŸ‘‡πŸ‘‡πŸ‘‡

  • Create a variable is the app.js file that would contain the content of the output. i.e const content = "This is a sample text file for Node JS tutorial"

specify the file paths and for the second parameter, we use the content variable. i.e writeFileSync("./files/output.txt", content) πŸ‘‡πŸ‘‡πŸ‘‡

run the command: node app.js. to see the output πŸ‘‡πŸ‘‡πŸ‘‡

Asynchronous nature of Node JS

Synchronous programming means that tasks are carried out one after the other in a step by step fashion, "Line by line" in the order which they are been written. You complete one process or instruction at a time.

Asynchronous programming is running multiple tasks, you don't have to wait for one to finish. i.e Lets replace the readFileSync() with readFile()πŸ‘‡πŸ‘‡πŸ‘‡

The readFile() method runs asynchronously. when we execute the above program, first of all, this line of code: const fs = require("fs") will be executed and will be executed in the main thread which is provided by JavaScript. Once this execution of the first line is complete, the next line of code: πŸ‘‡πŸ‘‡πŸ‘‡

will be executed but this code will be executed in the background, it will not be executed in the main thread instead it will run in the background. And since its not running on the main thread, the main thread is not blocked. This means the main thread is empty so the next line of code can be executed in the main thread and here we are executing: console.log("Reading file...") in the main thread and once its complete it will log the message in the output window. πŸ‘‡πŸ‘‡πŸ‘‡

The readFile πŸ‘‡πŸ‘‡πŸ‘‡

is still executing in the background but it did not block the execution of the next line of code. This is simply the definition of asynchronous programming.

The Node JS environment provides some APIs which can run synchronously or asynchronously. The readFileSync() is an API provided by node and this API runs synchronously while runs readFile() runs asynchronously

Next: Creating a web server...

S

Writing blog on 100 days of codingπŸ₯ΆπŸ₯Ά That's lot's of work man!!! All the best!!! Keep up Good work

1
P
Paks2y ago

I know right, thanks Buddy

1

More from this blog

Untitled Publication

36 posts