Node Js Inter View Question 2024
Node Js Inter View Question 2024
For anyone looking to excel in Node.js interviews this year, having a solid grasp of
both fundamental concepts and the latest advancements is crucial.
To assist you in your preparation, we've curated a definitive list of the top 50 Node.js
interview questions and their comprehensive answers. Let's embark on a journey to
master the essential principles and intricacies of Node.js development!
1. What is NodeJS?
10. What are processes and threads and How do they communicate between multiple
threads and processes?
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 1/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Level-2: Intermediate
25. What difference between fork and spawn and exec methods?
29. How to handle large file upload on node server? What is highWaterMark ?
Level-3: Expert
44. How can you improve the performance of the node applications?
45. What are the security best practices for node applications?
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 2/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
50. List out the use of the following npm modules (shrinkwrap, forever, dotenv,
nodemon, response-time, multer, body-parser, loadtest)
==============================================================
1. What is NodeJS?
It allows to execution of the javascript code on the server (outside of the browser)
on any machine. It is not a language & nor a framework. It's a Javascript runtime
environment.
Libuv is a multi-platform C library (that implements the event loop) that provides
support for asynchronous I/O.
The Event loop is single-threaded, which means that it can only execute one task
at a time. However, the event loop is also non-blocking, which means that it can
continue to execute other tasks while it is waiting for an I/O operation to complete.
This makes Node.js applications very responsive, as they can continue to handle
other requests even while they are waiting for a file to be read or a network request to
be completed.
Working: The way Libuv and the event loop work is based on the Reactor Pattern.
In this pattern, there is an Event queue and an Event demultiplexer. The loop (i.e.
the dispatcher) keeps listening for incoming I/O and for each new request, an event is
emitted.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 3/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
The event is received by the demultiplexer and it delegates work to the specific
handler (the way these requests are handled differs for each OS). Once the work is
done, the registered callback is enqueued on the Event queue. Then the callbacks are
executed one by one and if there is nothing left to do, the process will exit.
REPL stands for (READ, Eval, Print Loop). It represents a computer environment
like a window console or Unix shell where a command is entered and the system
responds with an output.
package.json is a manifest file that contains metadata about the project and
specifies its dependencies. It defines the project configuration. It includes details
such as the project name, version, entry point, script commands, and most
importantly, the list of dependencies and their versions.
Pipe: In Node.js, the "pipe" method is used to direct the output of one stream to
the input of another stream. It is commonly used with readable and writable
streams to efficiently transfer data from one source to another without explicitly
having to manage the data flow.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 4/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
const fs = require('fs');
readableStream.pipe(writableStream);
console.log(result); // Output: 24
Both concepts are fundamental to working with streams and asynchronous operations
in Node.js.
Control flow functions are used to dictate the order in which specific code blocks
or functions are executed. These functions are used to manage the flow of
execution within a program, enabling developers to handle asynchronous
operations, iterate through collections, handle conditional logic, and more.
Buffer is a temporary memory, mainly used by the stream to hold some data until
consumed. Buffer is mainly used to store binary data while reading from a file or
receiving packets over the network.
// a string
const str = "Hey. this is a string!";
// convert string to Buffer
const buff = Buffer.from(str, "utf-8");
console.log(buff); // <Buffer 48 65 79 2e ... 72 69 6e 67 21>
Stream is the object (abstract interface) that allows us to transfer data from
source to destination and vice-versa. It enables you to process large amounts of
data chunk by chunk, without having to load the entire data set into memory at once.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 5/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
a)Readable Stream: A Readable stream represents a source from which data can be
read. It emits "data" events whenever new data becomes available. An example of a
Readable stream is reading a file line by line:
const fs = require('fs');
const readline = require('readline');
const fs = require('fs');
writableStream.write('Hello, ');
writableStream.write('World!');
writableStream.end();
c)Duplex Stream: A Duplex stream is both readable and writable, allowing both
data input and output. A common example is a TCP socket, where data can be both
read from and written to. Here's a simple echo server using a Duplex stream:
server.listen(3000);
const fs = require('fs');
const zlib = require('zlib');
readableStream.pipe(gzipStream).pipe(writableStream);
10. What are processes and threads and How do they communicate between
multiple threads and processes?
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 6/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
When you run a Node.js application, you're essentially starting a process. Node.js
applications can be single-threaded, meaning they run in a single process, or
they can leverage the built-in clustering module to create multiple processes to
take advantage of multi-core systems.
if (cluster.isMaster) {
// Create a worker process for each CPU core
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
However, Node.js does use additional threads from a thread pool managed by the
libuv library to handle certain operations, such as file system operations.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 7/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
if (isMainThread) {
// This code is executed in the main thread
// Create new worker threads
const worker1 = new Worker(__filename);
const worker2 = new Worker(__filename);
here the main thread creates two worker threads using the Worker class. Each
worker thread listens for messages from the main thread using the on('message'
event listener and sends messages back to the main thread using
parentPort.postMessage().
When running this code, you would see messages exchanged between the main
thread and the worker threads, demonstrating the inter-thread communication.
Communication between Threads: you can pass data between threads using the
Worker and parentPort objects provided by the Worker Threads module.
// main.js
const { Worker } = require('worker_threads');
In this example, the main.js file creates a new worker thread using
Worker('./worker.js') and passes initial data to the worker using the workerData
option. In the worker.js file, the workerData is received from the main thread, and a
message is sent back to the main thread using parentPort.postMessage().
When the worker script is executed, calling parentPort.postMessage() will trigger the
'message' event in the main thread, and the data passed from the worker thread can
be accessed and processed accordingly.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 8/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
This approach allows threads to communicate with each other and pass data back
and forth as needed, enabling efficient multi-threaded data processing in Node.js.
The POST method is used to submit data to a server, the PUT method is used to
replace or create a resource at a specific URL, and the PATCH method is used to
apply partial modifications to a resource.
The POST method is commonly used for creating new resources or triggering
operations that are not idempotent, meaning that … the same request could result
in different outcomes based on the server state or implementation.
// myModule.js
const myFunction = () => {
console.log('This is my function');
};
module.exports.myFunction = myFunction;
module.exports.myVariable = myVariable;
// anotherModule.js
const myModule = require('./myModule');
Event-Driven
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 9/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Single-Threaded
Non-Blocking I/O
In a Node.js application, you can set up CORS handling using middleware such as
the 'cors' package or by manually adding the necessary CORS headers to responses.
This allows you to control which origins have permission to access resources in your
Node.js application.
When you visit a website, let's say "example.com", your web browser allows
JavaScript code running on "example.com" to make requests to the same domain.
This is part of the security protocol called the same-origin policy, and it's meant to
protect users' data from malicious attacks.
However, sometimes a web page might need to make requests to a different domain.
For example, let's say "example.com" needs to retrieve some data from
"api.otherdomain.com". This is where CORS comes into play.
console.log('Salt:', salt);
console.log('Hashed Password:', hash);
bcrypt Module: The bcrypt module is specifically designed for password hashing
using the bcrypt algorithm. It provides a convenient way to securely hash
passwords, a common requirement for user authentication systems. bcrypt
automatically handles the generation of salts, which enhances the security of the
hashed passwords.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 10/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
NPM is the default package manager for Node.js and is used to install, manage,
and publish packages. It is bundled with Node.js installation.
NPX is a tool that comes with NPM and is used to execute Node.js packages
without having to install them globally. It allows you to run packages directly from
the NPM registry or execute binaries from local node_modules/.bin folders
An LTS (Long Term Support) release is a specific version of the software that is
designated for long-term maintenance and support.
In the context of a Node.js server using the Express framework, req.params and
req.query are used to access different types of parameters passed in the URL.
// Route definition
app.get('/users/:id', (req, res) => {
const userId = req.params.id; // Access the "id" parameter from the URL
// Use userId to retrieve user data
});
If you have a route defined as /search, and a client makes a request like /search?
city=NewYork&active=true, you can access the query parameters city and active
using req.query.city and req.query.active respectively.
// Route definition
app.get('/search', (req, res) => {
const city = req.query.city; // Access the "city" query parameter
const active = req.query.active; // Access the "active" query parameter
// Use city and active for searching
});
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 11/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Dependencies are the packages that are required for the application to run in the
production environment.
DevDependencies are the packages that are only needed for development and
testing purposes. These packages include tools, libraries, and utilities that are
used during the development, testing, and build process, but are not required for the
application to function in the production environment.
Authentication is the process of validating the identity of a user or entity, through the
credentials, such as usernames, passwords, biometric data, or security tokens.
Using try-catch:
try {
// Code that may throw an error
const result = someFunction();
console.log(result);
} catch (error) {
console.error('An error occurred:', error);
}
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 12/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
A cron job is a time-based job scheduler. It allows users to schedule tasks (commands
or scripts) to run periodically at fixed times, dates, or intervals.
In Node.js, you can use the node-cron module to schedule jobs to run at specific
times.
The first argument '* * * * *' represents the cron expression for running the job every
minute. The second argument … is a function that will be executed when the
scheduled time is reached.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 13/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
25. What difference between fork and spawn and exec methods?
The fork method is specifically designed for creating child processes that
run Node.js modules. It is commonly used to create new instances of the
Node.js interpreter to run separate Node.js scripts.
With spawn, if you need to establish communication between the parent and
child processes, you have to manually set up the standard input and output
streams to exchange data and messages.
The exec method runs a command in a shell and buffers the output. It is
suitable for simple commands and scripts where the output is not excessively
large.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 14/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Node.js Global Objects are the objects that are available in all modules. Global
Objects are built-in objects that are part of JavaScript and can be used directly in the
application without importing any particular module.
1. global: The global object is the global namespace object in Node.js. It acts as a
container for global variables and functions. Any variable or function defined on
the global object becomes available across modules.
2. process: The process object provides information about the current Node.js
process and allows you to control the process. It contains properties such as
process.env, process.argv, and methods like process.exit().
3. console: The console object provides a simple debugging console that is similar
to the console mechanism provided by web browsers. It includes functions like
console.log(), console.error(), and console.warn().
4. Buffer: The Buffer class provides a way to work with binary data directly. Buffers
are used in Node.js to handle raw binary data for tasks such as reading from or
writing to the file system, dealing with network operations, or handling binary
data in other formats.
5. __filename: The __filename variable represents the name of the current file.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 15/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
The Event Loop is composed of the following six phases, which are repeated for as
long as the application still has code that needs to be executed:
1. Timers
2. I/O Callbacks
3. Waiting / Preparation
4. I/O Polling
5. setImmediate() callbacks
6. Close events
The Event Loop starts at the moment Node.js begins to execute your index.js
file, or any other application entry point.
These six phases create one cycle, or loop, which is known as a tick. A Node.js
process exits when there is no more pending work in the Event Loop, or when
process.exit() is called manually.
Some phases are executed by the Event Loop itself, but for some of them the main
tasks are passed to the asynchronous C++ APIs.
Phase 1: timers: The timers phase is executed directly by the Event Loop. At the
beginning of this phase, the Event Loop updates its own time. Then it checks a
queue, or pool, of timers. This queue consists of all timers that are currently set. The
Event Loop takes the timer with the shortest wait time and compares it with the Event
Loop's current time. If the wait time has elapsed, then the timer's callback is queued
to be called once the call stack is empty.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 16/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Phase 4: I/O polling (poll phase): This is the phase in which all the JavaScript code
that we write is executed, starting at the beginning of the file, and working down.
Depending on the code it may execute immediately, or it may add something to the
queue to be executed during a future tick of the Event Loop.
During this phase, the Event Loop is managing the I/O workload, calling the functions
in the queue until the queue is empty, and calculating how long it should wait until
moving to the next phase. All callbacks in this phase are called synchronously in the
order that they were added to the queue, from oldest to newest.
Note: this phase is optional. It may not happen on every tick, depending on the
state of your application.
If there are any setImmediate() timers scheduled, Node.js will skip this phase during
the current tick and move to the setImmediate() phase.
Phase 6: close events: This phase executes the callbacks of all close events. For
example, a close event of web socket callback, or when process.exit() is called.
This is when the Event Loop is wrapping up one cycle and is ready to move to the
next one. It is primarily used to clean the state of the application.
Together, the Reactor pattern and the Demultiplexer form the core of Node.js'
event-driven, non-blocking I/O model. This architecture allows Node.js to handle a
large number of concurrent operations efficiently, making it well-suited for building
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 17/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
29. How to handle large file upload on node server? What is highWaterMark ?
To handle large file uploads on a Node.js server, you can use the multer middleware,
which is a popular choice for handling multipart/form-data, including file uploads.
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Another third-party module that is commonly used for handling file uploads in Node.js
is busboy. busboy is a streaming parser for HTML form data for Node.js. It
provides a way to handle file uploads and other form data within HTTP requests.
busboy.on('finish', () => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('File uploaded successfully');
});
req.pipe(busboy);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}
}).listen(3000, () => {
console.log('Server is running on port 3000');
});
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 18/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
const fs = require('fs');
const readStream = fs.createReadStream('largeFile.txt', { highWaterMark: 16 *
1024 }); // Set highWaterMark to 16 KB
Middleware is the javascript function that has full access to HTTP request and
response cycle. Middleware comes in between your request and business logic. It is
mainly used to capture logs and enable rate limit, routing, authentication, and
security header.
const fs = require('fs');
const fs = require('fs');
readableStream.on('end', () => {
// All data has been read
});
Hashing is a one-way process used for data integrity and verification. It converts
input data into a fixed-size string of characters, typically a hexadecimal number. The
resulting string, known as a hash value or digest, is unique to the input data.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 19/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Hashing is indeed idempotent, meaning that for the same input, the resulting hash
value will always be the same. This property is desirable for ensuring data integrity
and security.
Example of hashing:
When the user sets or changes their password, you would hash their input using
bcrypt and then store the resulting hash in your database. When doing this,
bcrypt handles the addition of a salt to the password and the hashing process.
When the user attempts to log in, you would retrieve the stored hash from the
database and then use bcrypt's compare function to hash the password input by
the user and compare the resulting hash with the stored hash. If they match, the
input password is correct.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 20/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Performance Optimization
System-level Functionality
Cluster module:
When using the cluster module, a single "master" process is responsible for
managing multiple "worker" processes, distributing incoming connections among
them, and restarting workers as needed.
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
child_process module:
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 21/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
When using the child_process module, the parent process can spawn child
processes, communicate with them, and receive their results
asynchronously using event-driven mechanisms. This allows for parallel
execution of tasks and better utilization of system resources.
To read a file in Node.js, you can use the fs module's readFile function.
const fs = require('fs');
To write data to a file in Node.js, you can use the fs module's writeFile function.
const fs = require('fs');
To compress a file in Node.js, you can use the zlib module to create a GZIP archive.
const fs = require('fs');
const zlib = require('zlib');
readStream.pipe(gzip).pipe(writeStream);
writeStream.on('finish', () => {
console.log('File has been compressed');
});
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 22/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
async.series([
function(callback) {
// Perform asynchronous operation 1
callback(null, 'Result 1');
},
function(callback) {
// Perform asynchronous operation 2
callback(null, 'Result 2');
}
], function(err, results) {
// All tasks have been completed
console.log(results);
});
async.parallel([
function(callback) {
// Perform asynchronous operation 1
callback(null, 'Result 1');
},
function(callback) {
// Perform asynchronous operation 2
callback(null, 'Result 2');
}
], function(err, results) {
// All tasks have been completed
console.log(results);
});
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 23/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Create a directory named "locales" at the root of your project to store the locale-
specific translation files.
Inside the "locales" directory, create separate JSON files for each supported
language. For example, you might have "en.json" for English and "fr.json" for
French. Each file should contain key-value pairs for the translations.
{
"greeting": "Hello",
"welcome": "Welcome"
}
In your Node.js application entry file (e.g., app.js), configure the i18n module.
i18n.configure({
locales: ['en', 'fr'],
defaultLocale: 'en',
directory: __dirname + '/locales',
objectNotation: true
});
app.use(i18n.init);
app.listen(3000, () => {
console.log('Server running on port 3000');
});
You can now use the translations in your routes or views. For example, in an
Express route handler.
Promises and Observables are both used for managing asynchronous operations in
JavaScript, but there are several differences between the two:
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 24/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Observable: Lazily evaluates and does not trigger the asynchronous operation
until it has at least one subscriber interested in the emitted values. This allows
for more efficient use of resources when dealing with cold observables.
Cancellation:
Backward Compatibility:
Observable: Introduced later through libraries such as RxJS and is not part of
the core JavaScript language, although it has gained popularity, especially in the
context of reactive programming and complex asynchronous scenarios.
Example of Observable:
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 25/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
// Create an observable
const observable = new Observable((observer) => {
// Emit three values asynchronously with a delay
setTimeout(() => {
observer.next('First value');
}, 1000);
setTimeout(() => {
observer.next('Second value');
}, 2000);
setTimeout(() => {
observer.next('Third value');
// Complete the observable after emitting the third value
observer.complete();
}, 3000);
});
There are several libraries available in the Node.js ecosystem that facilitate validation,
such as Joi, Validator.js, Express-validator, and Yup. These libraries provide a rich set
of validation functions for checking data against predefined rules and constraints.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 26/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Emitter:
Emitters are commonly used in languages like JavaScript, where objects can be
event emitters, and they are central to frameworks like Node.js for handling
asynchronous events.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 27/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
/**
* Callback Events with Parameters
*/
const events = require('events');
const eventEmitter = new events.EventEmitter();
// Output
status 200 and ok
Dispatcher:
It acts as a message broker that receives messages from emitters and then
forwards them to the appropriate handlers, listeners, or components based on
predefined criteria.
Loadtest is a Node.js module that allows you to perform load testing on HTTP
endpoints. It provides a simple command-line interface for quick testing and also
allows for programmatic usage for more complex scenarios.
In this command:
You can also use loadtest programmatically in your Node.js application. Here's an
example of how you can perform a simple programmatic load test:
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 28/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
const options = {
url: 'http://yourwebsite.com',
maxRequests: 100,
concurrency: 10,
};
In Node.js, memory leaks can occur due to various reasons, and addressing them is
vital to maintain the performance and stability of Node.js applications. Some common
types of memory leaks in Node.js include:
2. Closures: Closures can keep references to local variables even after they are
no longer needed, preventing the garbage collector from reclaiming their
memory.
3. Forgotten Timers and Callbacks: Timers and callbacks that are not cleared
when they are no longer needed can hold references to objects, preventing their
disposal.
6. Event Emitters: If event emitters are not properly cleaned up after use, they
can create memory leaks. Subscribing to events without unsubscribing from
them can keep objects alive longer than necessary.
In your Node.js application, create a connection to the Redis server using the redis
package:
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 29/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Let's consider an example where we have a function that fetches user data from a
database and we want to cache this data using Redis. Here's a simplified
implementation using an Express.js route:
function fetchUserDataFromDatabase(userId) {
// Simulated database fetch
return {
id: userId,
name: 'John Doe',
// Other user data
};
}
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
In this example, when a request is made to fetch user data, the application first
checks if the data is present in the Redis cache. If it is, the cached data is returned. If
not, the data is fetched from the database, stored in the cache using client.setex(),
and then returned to the client.
44. How you can improve the performance of the node applications?
Use Streams: Utilize Node.js streams for data processing, especially when dealing
with large volumes of data. Streams enable data to be processed in chunks, reducing
memory usage and improving performance.
const fs = require('fs');
const readableStream = fs.createReadStream('input.txt');
const writableStream = fs.createWriteStream('output.txt');
readableStream.pipe(writableStream);
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 30/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Implement Caching: Cache frequently accessed data using in-memory solutions like
Redis to reduce the need for repeated database queries and enhance overall
application responsiveness.
// Caching data
const fetchUserData = (userId) => {
// Simulated data fetch from database
const userData = { id: userId, name: 'John Doe' };
// Store data in cache with expiration
client.setex(`user:${userId}`, 3600, JSON.stringify(userData));
return userData;
};
Utilize Worker Threads: For CPU-intensive tasks, consider using Node.js worker
threads to offload processing to separate threads and take advantage of multi-core
CPUs without blocking the main event loop.
if (isMainThread) {
// Main thread
const worker = new Worker(__filename);
worker.on('message', (msg) => {
console.log('Worker said:', msg);
});
} else {
// Worker thread
parentPort.postMessage('Hello from the worker!');
}
Scaling:
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 31/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
if (cluster.isMaster) {
const numCPUs = os.cpus().length;
// Index creation
db.collection('users').createIndex({ name: 1 });
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 32/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
function fetchUserData(userId) {
// Simulated data fetch from a database or API
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`User data for user ${userId}`);
}, 1000);
});
}
function fetchData() {
return new Promise((resolve, reject) => {
// Simulated asynchronous data fetching
setTimeout(() => {
resolve('Data fetched');
}, 1000);
});
}
fetchData()
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
Offload static content delivery to a CDN to reduce the load on the application
servers and improve content delivery performance.
45. What are the security best practices for node applications?
Dependency Management:
Validate and sanitize user input to prevent injection attacks, such as SQL
injection, NoSQL injection, and cross-site scripting (XSS). Leveraging libraries
like Joi or validator.js can facilitate robust input validation.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 33/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Secure Communication:
Security Headers:
Implement robust logging and monitoring solutions to track and analyze security
events, anomalous behaviors, and potential vulnerabilities within the application.
Conduct periodic security audits and penetration testing to identify and address
potential security flaws, vulnerabilities, and weaknesses in the application's
architecture and codebase.
Horizontal Scaling:
Use a load balancer to distribute incoming traffic across multiple instances of the
Node.js application. Each application instance can run on a separate server or
container, allowing the application to handle more concurrent requests.
Vertical Scaling:
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 34/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
Upgrade the server hosting the Node.js application with more resources, such
as CPU, memory, or storage, to support increased loads and processing
requirements.
Clustering:
Use containerization technologies like Docker to package the application and its
dependencies into containers. Orchestrate these containers with tools like
Kubernetes to manage and scale them across a cluster of machines.
Serverless Architecture:
Database Scaling:
Scale the database layer independently to handle increased data volume and
read/write operations. This can involve sharding, replication, using distributed
databases, or utilizing managed database services that support auto-scaling.
Prevention From DOS and DDOS Attacks: To prevent Denial of Service (DoS) and
Distributed Denial of Service (DDoS) attacks in Node.js applications, there are several
strategies and best practices that can be employed.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 35/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
1. Rate Limiting: Implement rate limiting to restrict the number of requests from a
client within a given time frame. This can be done using middleware such as
express-rate-limit in Express.js to prevent an attacker from overwhelming the
server with a large number of requests.
2. Monitoring and Anomaly Detection: Use monitoring tools to track the normal
behavior and traffic patterns of your application. Implement anomaly detection to
identify and respond to unusual spikes in traffic. Services like Amazon
CloudWatch, New Relic, or similar monitoring tools can help in detecting
anomalies.
3. Use of CDN and Proxy Services: Content Delivery Networks (CDNs) and
proxy services such as Cloudflare can help absorb and mitigate DDoS
attacks by filtering traffic and serving as a protective layer between the
attackers and your application.
4. Web Application Firewall (WAF): Implement a WAF that can detect and filter
out malicious traffic, preventing common web-based attacks including those
used in DoS and DDoS attacks.
7. Regular Security Audits and Updates: Regularly audit and update your
application and infrastructure for security vulnerabilities. Stay updated with
security patches and best practices to mitigate known attack vectors that
can be exploited by attackers.
SQL injection is a type of security vulnerability that occurs when an attacker is able
to manipulate input that is passed into a SQL query, leading to the unintended
execution of malicious SQL code.
SQL injection attacks typically exploit web applications or other software that
interact with a backend database. Attackers can inject malicious SQL code into
input fields such as login forms, search boxes, or any other user-controllable data
input.
2. Input Validation and Sanitization: Validate and sanitize user input to ensure
that it conforms to the expected format and does not contain any malicious SQL
code. Libraries like validator.js or frameworks like Express-validator can be
used for input validation and sanitization.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 36/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
4. Escaping User Input: If you are manually constructing SQL queries, make sure
to escape user input using the appropriate escaping functions provided by your
database library. This prevents user input from being interpreted as SQL
commands.
6. Regular Security Patching: Keep your database software and related libraries
up to date with the latest security patches to mitigate known vulnerabilities that
could be exploited by attackers.
To write test cases for the backend in Node.js, you can utilize testing frameworks like
Mocha or Jest and chai.
Mocha is a testing framework that provides functions that are executed according in a
specific order, and that logs their results to the terminal window.
Chai is an assertion library that is often used alongside Mocha. It provides a number
of assertion methods that can be used to test the output of functions and code
snippets.
Example: Suppose you have a signin function that takes a username and password,
and it returns a promise that resolves to a user object if the signin is successful, and
rejects with an error if the signin fails.
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 37/38
24/12/2024, 15:20 Top 50 NodeJS Interview Questions and Answers for 2024 | by Ravi Sharma - Freedium
// Test case 2
it('should return an error for invalid credentials', async () => {
try {
await signin('user', 'wrong-password');
assert.fail('Expected the signin to reject with an error');
} catch (error) {
assert.instanceOf(error, Error);
assert.equal(error.message, 'Invalid username or password');
}
});
});
50. List out the use of the following npm modules (shrinkwrap, forever, dotenv,
nodemon, response-time, multer, body-parser,)
https://freedium.cfd/https://javascriptcentric.medium.com/top-50-nodejs-interview-questions-and-answers-for-2024-5e460dac7852 38/38