NodeJs Questions and Answers
NodeJs Questions and Answers
1. Blocking functions - In a blocking operation, all other code is blocked from executing until an
I/O event that is being waited on occurs. Blocking functions execute synchronously.
2. Non-blocking functions - In a non-blocking operation, multiple I/O calls can be performed
without the execution of the program being halted. Non-blocking functions execute
asynchronously.
1|P age
7. What are streams in Nodejs Explain the different types of streams present in
Nodejs?
Streams are objects that allow the reading of data from the source and writing of data to the
destination as a continuous process.
is a form of Duplex stream that performs computations based on the available input.
2|P age
13. Explain libuv?
Libuv is a multi-platform support library of Node.js which majorly is used for asynchronous I/O. It
was primarily developed for Node.js, with time it is popularly practiced with other systems like as
Luvit, pyuv, Julia, etc. Libuv is basically an abstraction around libev/ IOCP depending on the platform,
providing users an API based on libev. A few of the important features of libuv are:
14. List down the two arguments that async.queue takes as input?
Below are the two arguments that async.queue takes as input - Task Function & Concurrency Value
3|P age
specific asynchronous I/O tasks which execute in the background and don’t usually execute any JS
code or hinder with the main event loop in the application. If you still want to use the threading
concept in your application you have to include a module called ChildProcess explicitly.
o Readable: Used for reading large chunks of data from the source.
o Writeable: Use for writing large chunks of data to the destination.
o Duplex: Used for both the functions; read and write.
o Transform: It is a duplex stream that is used for modifying the data.
4|P age
22. Explain the reason as to why Express app and server folder must be kept separate?
Express ‘app’ and ‘server’ must be kept separate as by doing this, you will be separating the API
declaration from the network related configuration which benefits in the below listed ways:
o It allows testing the API in-process without having to perform the network calls
o Faster testing execution
o Getting wider coverage metrics of the code
o Allows deploying the same API under flexible and different network conditions
o Better separation of concerns and cleaner code
N-API
nan direct use of internal V8
libuv
Node.js libraries
27. How can we spawn the child process asynchronously without blocking the Nodejs
event loop?
child_process.spawn() method spawns the child process asynchronously, without blocking
the Node.js event loop, the child_process.
spawnSync() function provides equivalent functionality in a synchronous manner that blocks
the event loop until the spawned process either exits or is terminated
5|P age
28. How can we take advantage of multi-core system in Nodejs as nodejs works on
single thread?
We can use node js cluster to use multicores in the hardware, the cluster module allows easy
creation of child processes that all share server ports
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
$ node server.js
6|P age
Master 3596 is running
1. console.clear() will clear only the output in the current terminal viewport for the Node.js
binary.
2. console.error([data][, ...args]) Prints to stderr with newline. Multiple arguments can be
passed, with the first used as the primary message and all additional used as substitution
3. console.table(tabularData[, properties]) a table with the columns of the properties of
tabularData (or use properties) and rows of tabularData and log it.
const fs = require('fs');
There are a few methods like
7|P age
filename
clearImmediate(immediateObject)
clearInterval(intervalObject)
clearTimeout(timeoutObject)
console
exports
global
module
process
queueMicrotask(callback)
require()
setImmediate(callback[, ...args])
TextDecoder
TextEncoder
URL
URLSearchParams
WebAssembly
const os = require('os');
8|P age
36. Which are the areas where it is suitable to use NodeJS?
I/O bound Applications
37. Which are the areas where it is not suitable to use NodeJS?
it’s not suitable for heavy applications involving more of CPU usage
Fast in Code execution – Node.js uses the V8 JavaScript Runtime engine, the one which is used by
Google Chrome. Node has a wrapper over the JavaScript engine which makes the runtime engine
much faster and hence processing of requests within Node.js also become faster.
Single Threaded but Highly Scalable – Node.js uses a single thread model for event looping. The
response from these events may or may not reach the server immediately. However, this does not
block other operations. Thus making Node.js highly scalable. Traditional servers create limited
threads to handle requests while Node.js creates a single thread that provides service to much larger
numbers of such requests.
Node.js library uses JavaScript – This is another important aspect of Node.js from the developer’s
point of view. The majority of developers are already well-versed in JavaScript. Hence, development
in Node.js becomes easier for a developer who knows JavaScript.
There is an Active and vibrant community for the Node.js framework – The active community
always keeps the framework updated with the latest trends in the web development.
No Buffering – Node.js applications never buffer any data. They simply output the data in chunks.
9|P age
o READ - It Reads the input from the user, parses it into JavaScript data structure and then
stores it in the memory.
o EVAL - It Executes the data structure.
o PRINT - It Prints the result obtained after evaluating the command.
o LOOP - It Loops the above command until the user presses Ctrl+C two times.
40. Can you write CRUD operations in Node js without using frameworks?
Yes, we can use inbuilt http library for that, here is a simple code for the same:
Node.Js :
It is a server-side platform for developing client-server applications. For example, if we’ve to build an
online employee management system, then we won’t do it using client-side JS. But the Node.js can
certainly do it as it runs on a server similar to Apache, Django not in a browser.
10 | P a g e
It is a client-side scripting technique, primarily designed for rendering the contents of a page without
refreshing it. There are a no. of large companies utilizing AJAX such as Facebook and Stack Overflow
to display dynamic content.
JQuery :
It is a famous JavaScript module which complements AJAX, DOM traversal, looping and so on. This
library provides many useful functions to help in JavaScript development. However, it’s not
mandatory to use it but as it also manages cross-browser compatibility, so can help you produce
highly maintainable web applications.
When an EventEmitter instance encounters an error, it emits an “error” event. When a new listener
gets added, it fires a “newListener” event and when a listener gets removed, it fires a
“removeListener” event.
EventEmitter provides multiple properties like “on” and “emit”. The “on” property is used to bind a
function to the event and “emit” is used to fire an event. .
The Child processes always have three streams <child.stdin>, child.stdout, and child.stderr. The
stream of the parent process shares the streams of the child process.
Node.js provides a <child_process> module which supports following three methods to create a
child process.
**EXEC** – <CHILD_PROCESS.EXEC> METHOD RUNS A COMMAND IN A SHELL/CONSOLE AND BUFFERS THE OUTPUT.
**FORK** – <CHILD_PROCESS.FORK> IS A SPECIAL CASE OF THE SPAWN() METHOD TO CREATE CHILD PROCESSES.
11 | P a g e
44. What do you mean by Asynchronous API?
All APIs of Node.js library are aynchronous that is non-blocking. It essentially means a Node.js based
server never waits for a API to return data. Server moves to next API after calling it and a notification
mechanism of Events of Node.js helps server to get response from the previous API call.
Asynchronous and Event Driven All APIs of Node.js library are asynchronous that is non-
blocking. It essentially means a Node.js based server never waits for a API to return data.
Server moves to next API after calling it and a notification mechanism of Events of Node.js
helps server to get response from the previous API call.
Very Fast Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in
code execution.
Single Threaded but highly Scalable − Node.js uses a single threaded model with event
looping. Event mechanism helps server to respond in a non-bloking ways and makes server
highly scalable as opposed to traditional servers which create limited threads to handle
requests. Node.js uses a single threaded program and same program can services much
larger number of requests than traditional server like Apache HTTP Server.
No Buffering − Node.js applications never buffer any data. These applications simply output
the data in chunks.
12 | P a g e
49. How to check the already installed dependencies which are globally installed using
npm?
npm ls -g
13 | P a g e
55. How Node prevents blocking code?
By providing callback function. Callback function gets called whenever corresponding event
triggered.
When an EventEmitter instance faces any error, it emits an 'error' event. When new listener is
added, 'newListener' event is fired and when a listener is removed, 'removeListener' event is fired.
EventEmitter provides multiple properties like on and emit. on property is used to bind a function
with the event and emit is used to fire an event.
14 | P a g e
60. Which module is used for buffer-based operations?
buffer module is used for buffer-based operations.
15 | P a g e
66. What is Node.js and how it works?
Node.js is a virtual machine that uses JavaScript as its scripting language and runs Chrome’s V8
JavaScript engine. Basically, Node.js is based on an event-driven architecture where I/O runs
asynchronously making it lightweight and efficient. It is being used in developing desktop
applications as well with a popular framework called electron as it provides API to access OS-level
features such as file system, network, etc.
68. How is Node.js better than other frameworks most popularly used?
Node.js provides simplicity in development because of its non-blocking I/O and even-based model
results in short response time and concurrent processing, unlike other frameworks where
developers have to use thread management.
It runs on a chrome v8 engine which is written in c++ and is highly performant with constant
improvement.
Also since we will use Javascript in both the frontend and backend the development will be much
faster.
And at last, there are ample libraries so that we don’t need to reinvent the wheel.
69. Explain the steps how “Control Flow” controls the functions calls?
1. Control the order of execution
2. Collect data
3. Limit concurrency
4. Call the following step in the program.
16 | P a g e
71. What are the advantages of using promises instead of callbacks?
The main advantage of using promise is you get an object to decide the action that needs to be
taken after the async task completes. This gives more manageable code and avoids callback hell.
74. List down the two arguments that async.queue takes as input?
Task Function
Concurrency Value
75. How does Node.js overcome the problem of blocking of I/O operations?
Since the node has an event loop that can be used to handle all the I/O operations in an
asynchronous manner without blocking the main function.
For example, if some network call needs to happen it will be scheduled in the event loop instead of
the main thread (single thread). And if there are multiple such I/O calls each one will be queued
accordingly to be executed separately (other than the main thread).
Thus, even though we have single-threaded JS, I/O ops are handled in a nonblocking way.
17 | P a g e