node js interview questions
node js interview questions
js Interview Questions
A list of top frequently asked Node.js interview questions and answers are given below.
1) What is Node.js?
Node.js is Server-side scripting which is used to build scalable programs. It is a web
application framework built on Google Chrome's JavaScript Engine. It runs within the
Node.js runtime on Mac OS, Windows, and Linux with no changes. This runtime facilitates
you to execute a JavaScript code on any machine outside a browser.
o Node.js is asynchronous and event-driven. All API?s of Node.js library are non-
blocking, and its server doesn't wait for an API to return data. It moves to the next
API after calling it, and a notification mechanism of Events of Node.js responds to
the server from the previous API call.
o Node.js is very fast because it builds on Google Chrome?s V8 JavaScript engine. Its
library is very fast in code execution.
o Node.js is single threaded but highly scalable.
o Node.js provides a facility of no buffering. Its application never buffers any data. It
outputs the data in chunks.
o Client Layer: The Client layer contains web browsers, mobile browsers or
applications which can make an HTTP request to the web server.
o Server Layer: The Server layer contains the Web server which can intercept the
request made by clients and pass them the response.
o Business Layer: The business layer contains application server which is utilized by
the web server to do required processing. This layer interacts with the data layer
via database or some external programs.
o Data Layer: The Data layer contains databases or any source of data.
7) What do you understand by the term I/O?
The term I/O stands for input and output. It is used to access anything outside of your
application. The I/O describes any program, operation, or device that transfers data to or
from a medium or another medium. This medium can be a physical device, network, or
files within a system.
I/O is loaded into the machine memory to run the program once the application starts.
The following table specifies the crucial differences between JavaScript and Node.js:
Utility JavaScript is used for any client-side activity for a web Node.js is used for accessin
application. any non-blocking oper
operating system.
Running Engine The running engine for JavaScript is Spider monkey The running engine for Nod
(Firefox), JavaScript Core (Safari), V8 (Google Chrome).
Chrome), etc.
Browser JavaScript can only be run in browsers. The Node.js code can be
compatibility browser.
Platform JavaScript is basically used on the client-side and is Node.js is mostly used on
dependency used in frontend development. and is used in server-side d
HTML JavaScript is capable enough to add HTML and play Node.js is not compatible
compatibility with the DOM. HTML tags.
Examples Some examples of the JavaScript frameworks are Some examples of the Nod
RamdaJS, TypedJS, etc. Lodash, express, etc. We
these modules from npm.
Written in JavaScript is the upgraded version of ECMA script Node.js is written in C, C++
that uses Chrome's V8 engine and is written in C++.
11) Explain the working of Node.js?
The workflow of a Node.js web server typically looks like the following diagram. Let us see
the flow of operations in detail:
o According to the above diagram, the clients send requests to the webserver to
interact with the web application. These requests can be non-blocking or blocking
and used for querying the data, deleting data, or updating the data.
o js receives the incoming requests and adds those to the Event Queue.
o After this step, the requests are passed one by one through the Event Loop. It
checks if the requests are simple enough not to require any external resources.
o The event loop then processes the simple requests (non-blocking operations), such
as I/O Polling, and returns the responses to the corresponding clients.
o A single thread from the Thread Pool is assigned to a single complex request. This
thread is responsible for completing a particular blocking request by accessing
external resources, such as computation, database, file system, etc.
o Once the task is completed, the response is sent to the Event Loop that sends that
response back to the client.
12) How can you manage the packages in your Node.js project?
We can manage the packages in our Node.js project by using several package installers
and their configuration file accordingly. Most of them use npm or yarn. The npm and yarn
both provide almost all libraries of JavaScript with extended features of controlling
environment-specific configurations. We can use package.json and package-lock.json to
maintain versions of libs being installed in a project. So, there is no issue in porting that
app to a different environment.
Example:
1. getData(function(a){
2. getMoreData(a, function(b){
3. getMoreData(b, function(c){
4. getMoreData(c, function(d){
5. getMoreData(d, function(e){
6. ...
7. });
8. });
9. });
10. });
11. });
o Internet of Things
o Real-time collaboration tools
o Real-time chats
o Complex SPAs (Single-Page Applications)
o Streaming applications
o Microservices architecture etc.
19) Which is the best tool we can use to assure consistent code
style in Node.js?
ESLint tool is one of the best tools we can use with any IDE to ensure a consistent coding
style. It also helps in maintaining the codebase.
The front-end development includes everything that The back-end development generally includes
attributes to the visual aspects of a web application. communicates with the database to serve the u
HTML, CSS, Bootstrap, jQuery, JavaScript, AngularJS, Java, PHP, Python, C++, Node.js, etc., are t
and React.js are essential front-end development required for back-end development.
technologies.
Examples of some front-end frameworks are Examples of some back-end frameworks are
AngularJS, React.js, jQuery, Sass, etc. Rails, Laravel, Spring, etc.
22) What are the modules in Node.js? Which are the different
modules used in Node.js?
In Node.js applications, modules are like JavaScript libraries and include a set of functions.
To include a module in a Node.js application, we must use the require() function with the
parentheses containing the module's name.
Node.js has several modules which are used to provide the basic functionality needed for
a web application. Following is a list of some of them:
Core Description
Modules
HTTP: The HTTP module includes classes, methods, and events to create a Node.js HTTP se
util: The util module includes utility functions required in the application and is very usefu
url: The url module is used to include the methods for URL parsing.
fs: The fs module includes events, classes, and methods to handle the file I/O operation
stream: The stream module is used to include the methods to handle streaming data.
query string: The query string module is used to include the methods to work with a query string
zlib: The zlib module is used to include the methods to compress or decompress the
application.
Syntax:
o I/O operations
o Heavy computation
o Anything requiring blocking
32) What is REPL in Node.js?
REPL stands for Read Eval Print Loop. It specifies a computer environment like a window
console or Unix/Linux shell where you can enter a command, and the computer responds
with an output. It is very useful in writing and debugging the codes. REPL environment
incorporates Node.js.
1. $ node
2. > 100 + 200
3. 300
4. > 100 + ( 200 * 300 ) - 400
5. 59700
6. >
Read: It reads user's input; parse the input into JavaScript data-structure and stores in
memory.
Loop: It loops the above command until user press ctrl-c twice to terminate.
1. C:\Nodejs_WorkSpace>node
2. > var x = 10
3. undefined
4. > var y = 20
5. undefined
6. > x + y
7. 30
8. > var sum = _
9. undefined
10. > console.log(sum)
11. 30
12. undefined
13. >
o JSLint
o JSHint
o ESLint
o JSCS
Syntax:
1. punycode = require('punycode');
Syntax:
48) What are the key differences between Angular and Node.js?
Key differences between Angular and Node.js:
Angular Node.js
Angular is a structural front-end development framework for Node.js is a cross-platform, run-t
developing dynamic web apps. environment for applications writte
language.
Angular is entirely written in TypeScript language. Node.js is written in C, C++, and JavaS
Angular is used for building single-page, client-side web Node.js is used for building fast and sca
applications. and server-side networking application
Angular is easy to use. The developers need to add the Node.js is slightly complicated to
Angular file to use it in their applications. developers need to install Node.js on
system.
Angular split a web application into MVC components. Here, Node.js generates database queries an
the models and views are much simpler than what is found in driven nature of JavaScript to suppo
other JavaScript client-side frameworks. operations, making the platform efficie
Angular is ideal for creating highly active and interactive web Node.js is the best for developing sma
apps.
Example:
o The readFile() process is a fully buffered process that returns the response only
when the complete file is pushed into the buffer and is read. This process is called
a memory-intensive process, and in the case of large files, the processing can be
very slow.
o On the other hand, the createReadStream() is a partially buffered process that
treats the entire process as an event series. The entire file is split into chunks and
then processed and sent back as a response one by one. After completing this step,
they are finally removed from the buffer. Unlike the readFile process, the
createReadStream process is effective for the processing of large files.
To use it with any previous versions, you have to use the following code:
Syntax:
1. punycode = require('punycode');