What is Node.js?
Node.js is an Open source, cross-platform web application JavaScript runtime environment built on Google Chrome’s V8 JavaScript engine.
Node.js comes with a runtime environment on which a JavaScript-based script can be interpreted and executed. This runtime allows running a JavaScript code outside of a browser.


Why use Node.js?
Some of the advantages of Node.js include:
a) Node.js library is very fast as it is built on Google Chrome’s V8 JavaScript engine.

b) All APIs in the Node.js library are asynchronous, which means a Node.js based server never waits for an API to return data rather, it moves to the next API after calling it.
c) Node.js uses a single-threaded model with the event looping. The event mechanism helps the server respond in a non-blocking way which makes the server highly scalable.

d) Node.js is portable. It is available in different operating systems like Windows, Linux, macOS, Solaris, freeBSD etc.


Explain the flow diagram of Node.js?
A web server using Node.js has a workflow that typically looks like the below diagram.

The above diagram states that

a) The clients send requests to the webserver to interact with the web application. The requests can be blocking or non-blocking.

b) After receiving all the requests, js add those requests to the event queue.

c) After that, the requests are passed through the event loop. It also checks if the requests require any external resources.

d) The event loop processes the non-blocking requests and returns the responses.

e) For a blocking request, a single thread from the thread pool is assigned. This thread is responsible for completing a particular blocking request by accessing external resources like computationdatabasefile system etc.

f) After completing the task, the response is sent back to the event loop that sends the response back to the client.


Explain why Node.js is single-threaded?
Node.js was created for experimenting the async processing. The theory behind doing so was that the async processing on a single thread under typical web loads could provide more performance and scalability than the typical thread-based implementation.


How many types of API functions are available in Node.js?
In Node.js, there are two types of API functions:

a) Asynchronous and Non-blocking functions.
b) Synchronous, Blocking functions.


Explain what the callback is in Node.js?
A callback in Node.js is a function that is called after a given task. Thus it helps in preventing any blocking.


Explain what the Event Loop is in Node.js?
Despite being a single-threaded application, Node.js supports concurrency through the concept of events and callbacks.
As every API of Node.js is asynchronous, it uses the async function calls to maintain the concurrency.
The event loop allows Node.js to perform non-blocking operations despite being single-threaded. It is an endless loop that waits for tasks from the event queue, executes them and then sleeps until it receives the next task.


What is REPL in the context of Node?
The REPL stands for Read Eval Print Loop.
REPL represents a computer console where a command is given, and the system responds with an output.

Node.js also comes with a REPL environment which is described below,
a)Read: Read and parse the input.
b) Eval: Evaluates the data structure.
c) Print: Prints the result.
d) Loop: Loops the above command until the user terminates.


What is the use of the “_” variable in REPL?
The “_” variable is used to get the last result.


Explain what the first-class function in JavaScript is?
The first-class functions in JavaScript are treated like any other variable. These first-class functions can be passed as a parameter to any other function or return any other function.


What is NPM?
NPM stands for Node Package Manager. It is responsible for managing packages and modules for Node.js.


What is the main functionality of the NPM?
The NPM mainly provides two main functionalities:
a)  It provides online repositories for Node.js packages or modules which are searchable on search.nodejs.org
b) It provides a common line utility to install packages, and also it manages Node.js versions and dependency.


What do you mean by the modules in Node.js?
Modules in Node.js are the block of encapsulated code that perform some functionality. It can be a single file or a set of files. It can be used in Node.js applications to include a set of functions. Modules are essential from a programmer’s perspective as you can reuse them in a different code section.

Modules can be of different types like the core modules, local modules and third-party models.
Core modules are the built-in modules that come from the Node.js installation.
Local modules are the user-defined modules created locally in your Node.js application.
Third-party modules are available online and can be installed in the Node.js application using npm.

To include a module in Node.js, you need to use the “require()” function with the module name.
Like, If you want to include the “http” core module in your Node.js module,

var module = require(‘http’);


Explain different core modules in Node.js?
The different core modules of Node.js are discussed below:


What is the purpose of using the module. Exports in Node.js?
Modules in Node.js is the block of encapsulated code that can be parsed by moving all relevant functions into a single file.
The “module.exports” exports a module that lets the module be imported into another file with a needed keyword.


What are some of the most commonly used libraries in Node.js?
There are mainly two commonly used libraries in Node.js:

a)Express JS: Express is a flexible Node.js application framework that provides many features to develop web and mobile applications.
b) Mongoose: Mongoose is a Node.js application framework used to connect an application to a database.


What is the “package.json” file?
A package.json file defines the properties of a package, and it is present in the root directory of your Node.js application.

When you create your Node application by running the npm init command, it automatically creates a package.json file in the root directory of your NOde application.


How do you install, update and delete a dependency?
Installing a dependency:npm install dependency_name
Updating a dependency: npm update
Deleting a dependency: npm uninstall dependency_name


How do you create a server in Node.js?
There are a few steps to be followed to create a server in Node.js:
a) First, import the HTTP module.
b) Then, use the createServer() function that takes a callback function with request and response parameters.
c) Listen for any incoming requests.

var http = require(‘http’);
var server = http.createServer(function (req, res) { });
server.listen(5000);


What are the different types of HTTP requests in Node.js?
HTTP provides different types of methods that are used to perform different actions. The request methods include


What is the buffer class in Node.js?
The buffer class in Node.js is defined to handle and store the raw binary data that is similar to an array of integers. Each buffer corresponds to a raw memory allocation outside of V8. Unlike arrays, they are not resizable.


What is piping in Node.js?
Piping is a mechanism used to connect the output of one stream to another stream. The main purpose of the piping is to retrieve data from one stream and pass it to another stream.


What is callback hell?
Callback hell in Node.js is the situation in which we have complex nested, unreadable, unmanageable callbacks, making the code harder and more complex to read, test, and debug.


What do you understand by the term fork in Node.js?
Typically, a fork is used to spawn child processes. In Node.js, fork() is used to create new instances of the V8 engine. Here multiple workers run on a single node code base for multiple tasks.

Back to list

Leave a Reply

Your email address will not be published. Required fields are marked *