Quick start guide to Node.Js: Part-3

Devanshu Tiwari
3 min readAug 31, 2021

Script Processor Mode and REPL mode in Node.Js

As can be seen from the above image, we can execute a script using the script processor mode of Node.Js. We need to be in the directory where our script is present. Then executing the command node <script-name> will run our script and we will see the output in the console. Here an event loop is initialised in background which manages the execution of the asynchronous code of our script.
The second mode of Node.Js is the REPL : Read, Evaluate, Print, Loop. Its very similar to the console we get in browser to execute browser Api’s and javascript. Here in REPL mode we can run all Api functions provided by node environment along with our Javascript Code.
Read: Because it expects us to enter code expression and then parses it: (2+2)
Evaluate: Evaluates our expression
Print: Prints the Result to console
Loop: Ready again to take some input expression.

Now, lets get started with the Buffer class which is a global in Node.js, which means that we don’t need to require it.

Buffer in Node.js

In Node, we can interact with the file system using the fs module, make network requests using the http module. The files that we deal with in our digital machines stores data in binary form 1’s and 0’s. These files are not loaded at once in memory, infact that is exactly why Buffer class in needed!
Because Javascript do not have a byte type, so extra work needs to be done to interact with this binary data.
The data coming in streams, needs to be stored at some temporary place (also known as a buffer), which usually happens when the speed of data processing is greater than the speed of download. We all have experienced this at some point in our lives when we watch some live streaming video, it can buffer.
It is the Buffer class that provides methods in Node to deal with binary streams of data. To achieve this, we have various methods in Buffer class.
Some important functions in Buffer Class:
Buffer.alloc(10)- This will allocate 10 bytes of space in RAM.
Buffer.allocUnsafe(10): Similar to alloc( ), but can contain random binary data that needs to be overwritten
Buffer.from(‘Hello from dev!’)

toJSON( )- Converts buffer data to a Json Object.
toString( )- converts buffer data to a string using utf-8 encoding by default.

Why do we need to know about Buffer class?

Because when we start working on file-system module and http module, it is the Buffer class, which handles all the interactions in these modules, to interact with streams of binary data. For a proper understanding and appreciation of how Node.Js handles stream of data, this introduction was indeed necessary. 😃

Now, that we have covered the basic Buffer module, we can start with File system module and http module! But even before that, in the next article, lets understand how to use external Node modules and the package.json file. Here’s the link to it: Quick start guide to Node.Js: Part-4

--

--

Devanshu Tiwari

If you can’t explain it simply, you don’t understand it well enough