REPL stands for “Read Eval Print Loop”. It is a computer environment where a command is entered, and the system delivers an output in an interactive mode. Node.js is bundled with the REPL environment. This feature helps to debug JavaScript codes.The tasks performed by it are as follows:
Read – Takes input, converts it into JavaScript data structure, and stores it in memory.
Eval – Takes and evaluates the structure of the data.
Print – Prints the result
Loop – The command is looped until the user presses ctrl+c twice.
For example- $ node
> 1 + 3
4
> 1 + ( 2 * 3 ) - 4
3
>
Package Manager in Node.js life cycle
Package manager in Node.js is also known as Node Package Manager (NPM). It is a command-line tool used to install, update or uninstall packages in the application. The NPM performs operations in two modes, local and global. In the local mode, operations are done on the local directory, which only affects that directory's application. Whereas, in the global model, the performed operations affect all the applications on the computer.
For example- The coding used to download the package called ‘upper case’ is
C:\User\Your Name>npm install upper-case
The Callbacks Concept in Node.js life Cycle
A call-back function in Node.js is used when the task is completed. This function helps in preventing any kind of blocking and running other codes in the meantime. The call-back concept allows it to process multiple requests without waiting for other functions to return the result.
For example- getDbFiles(store, function(files){
getCdnFiles(store, function(files){
})
})
Event loop in Node.js life cycle
The event loop is a core concept of Node.js. It helps a developer to learn non-blocking I/O and asynchronous processes. The event loop works, knowing the fact the JavaScript is single-threaded. A thread is automatically established when a Node program is launched. After this, an event loop is generated. This loop determines which operations are single-threaded and should be performed at any given time.
Phases of Event Loop:
“timer - pending callbacks - idle, prepare - poll (incoming, connections) - check - close callbacks - timer…”
Timers: Callbacks scheduled by setTimeout() or setInterval() are executed in this phase.
Pending Callbacks: I/O callbacks deferred to the next loop iteration are executed here.
Idle, Prepare: For internal use.
Poll: Gets new Input/Output events.
Check: This phase invokes the setIntermediate() callbacks.
Close Callbacks: It manages the close callbacks. E.g., socket.on(‘close’)
For example -
console.log("This is the first statement");
setTimeout(function(){
console.log("This is the second statement");
}, 1000);
console.log("This is the third statement");
Output:
This is the first statement
This is the third statement
This is the second statement
Buffers Module in Node.js life cycle
Buffer is defined as a space in memory that is used for storing binary data. These spaces of memory in Node.js are accessed with the help of a built-in buffer class. The buffers store a sequence of integers like an array in JavaScript. Also, a developer cannot change the buffer size once it is created. The module that handles the binary data stream is known as the buffer module.
For example- The syntax for creating buffers is
const buffer = Buffer.alloc(7);
// This will print out 7 bytes of zero:
// <Buffer 00 00 00 00 00 00 00>
Streams in Node.js life cycle
Streams are the data handling methods used to read or write input into output in sequential order. It is an interface for working with streaming data in Node.js. The streams can handle writing or reading files and exchanging information. The stream module offers an API for the implementation of the stream interface.
For example- The syntax for readable stream
const Stream = require('stream')
const readableStream = new Stream.Readable()
After writing this syntax, one can send data
readableStream.push('ping!')
readableStream.push('pong!')
File system in Node.js cycle
The file system is a module of Node.js that handles file operations like reading, creating, deleting, etc. It provides the functionality of file I/O by offering wrappers around the standard functions of POSIX. All the file system operations can have asynchronous or synchronous forms depending upon the users' requirements. The syntax for using file system operation is: var fs = require ('fs');
Some uses of the file system module are:
- Write files
- Append files
- Read files
- Close files
- Delete files
For example- The coding used for opening a file is
var fs = require("fs");
// Asynchronous - Opening File
console.log("Going to open file!");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
});