Node.js Developer Interview Questions
1. What is NPM?
NPM stands for Node Package Manager. It is used for command line environment
applied to install and manage Node.js Packages and repositories.
2. What is Closures?
Closure is a first-class function which is defined within another scope that has
access to all the variables within the outer scope. Global variables can be made
private with closures.
3.
Explain Modules in Node.js
Modules are simple or complex functionalities in JavaScript files which can be
reused throughout the Node.js platform. Each module in Node.js can be placed in a
.js file in a separate folder. Also, Node.js modules have their own context, so it
cannot interfere with others.
4. Explain V8 Engine
V8 is an open source JavaScript engine. It was developed by Google and is now
used in Google Chrome, Node.js Couchbase, and MongoDB. The engine compiles
JavaScript to machine code before executing it, instead of most other techniques.
The compiled code is further optimized dynamically at runtime, based on heuristics
of the code's execution profile.
5. Explain event loop in Node.js
The event loop (Event Loop) is a mechanism that allows Node.js to perform
non-blocking I / O operations (despite the fact that JavaScript is single-threaded) by
uploading operations to the system kernel.
6. Explain what is libuv in Node.js
libuv in Node.js is a cross-platform I/O abstraction library, written in C. It suppors
Windows IOCP, epoll(4), kqueue(2), and Solaris event ports.
libUV library is used for the following:
- cross-platform I / O operations, working with files, network
- main Node.js event loop support
7. Why Node is single threaded?
As single thread operations run better than multi-threaded ones, it is done in order to
improve Node.js general performance.
8.
What are event emitters in Node.js?
Node.js objects trigger events to support asynchronous execution of core APIs used.
Objects that emit events as called ‘event emitters’.
9.
What is a cluster?
Cluster is a process used to handle thread execution load while working with
multi-core systems.
10. Explain the difference between readFile and createReadStream in Node.js.
readFile loads the whole file into the memory you selected, and
fs.createReadStream reads the entire file in specified chunks of sizes. Using
createReadStream, the client receives the data faster since it is sent in chunks
while it’s being read. readFile though reads the whole file first before sending it to
the client.
11. Write the steps for setting up an Express.js application.
In order to set Express.js up, a user has to follow these steps:
1. Create a folder with the same name as the project is called.
2. Add package.json file to the folder.
3. Run “npm install” command to install all the libraries from package.json.
4. Create a file named ‘server.js’.
5. Create a “router” file inside the package consisting of a folder named index.js.
6. To the package consisting of index.html file add “App” one.
The installation of Express.js is done.
12.
Since Node is a single threaded process, how to make use of all CPUs?
It’s possible to take advantage of all CPUs by using the cluster module. It contains a
set of properties that help to create programs which use all the CPUs of multi-core
systems.
13. How can you use middleware in Express?
Middleware in Express.js is a function that applies to the routing process, performing
operations at some points. It’s widely used for editing request and response objects
as well as terminating the request before it reaches the route handler code.
In order to set a middleware up, you need to invoke app.use() for each new layer
you’d like to add. Middleware in Express.js can be the same for all paths or used
only for a specific path your server handles.
14. Explain the difference between process nextTick() and setImmediate()?
The main difference between nextTick() and setImmediate() processes is that
setImmediate() queus its callbacks on the even loop and nextTick() does not. Thus,
as nextTick() callbacks execute before the even loop, they run prior to
setImmediate() ones.