loader spinner

Questions and answers of node.js in 2020

December 08, 2019 12:55pm - 11 min read

Last updated on: April 04, 2020 4:42am

Beginner level

Differentiate between JavaScript and Node.js

Javascript is a programming language and Node is not a language, hence it is an interpreter and runtime environment for Javascript. Before Node, Javascript only runs in the browser and there was no way to run Javascript on server or any other medium. Node brings this options where the developer can run Javascript codes outside of the browser, on any machines, any OS. To run Javascript, you must install Node first and then you can run Javascript code. So basically Node allows creating an environment where you can play around with Javascript. Javascript uses different engines for different browsers. For example, Spider monkey is used in firefox, Javascript core in safari and V8 in Google chrome browser. Node only uses Google’s V8 engine.

What Is Node.js?

Nodejs is an open-source, cross-platform and JavaScript run-time environment. It is a lightweight framework used to develop server-side web applications and extends JavaScript API to offer usual server-side functionalities. Node.js is built upon Google Chrome’s V8 run-time—written in C++, built for multiple operating systems and super fast.
Node.js is single-threaded, that employs a concurrency model based on an event loop. It doesn’t block the execution instead registers a callback which allows the application to continue. It means Node.js can handle concurrent operations without creating multiple threads of execution.
Node.js is used for creating large scale application development, mostly used for video streaming websites, single page applications, and other web applications.
The following are the areas where it’s perfect to use Node.js.

  • I/O bound Applications
  • Data Streaming Applications
  • Data Intensive Real-time Applications (DIRT)
  • JSON APIs based Applications
  • Single Page Applications

At the same time, it’s not suitable for heavy applications involving more CPU usage.

What Are The Key Features Of Node.Js? or Major benefits of Node.js

Fast – Node.js uses the V8 JavaScript Runtime engine which makes the runtime engine much faster and hence processing of requests within Node.js also becomes faster.
Asynchronous – Asynchronous event driven IO helps concurrent request handling. All APIs of Node.js are asynchronous. It means if a Node receives a request for some Input/Output operation, it will execute that operation in the background and continue with the processing of other requests. Thus it will not wait for the response from the previous requests.
Scalable – 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.
Open Source – Node.js has an extensive open source community which has contributed to producing some excellent modules to add additional capabilities to Node.js applications. 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.
No Buffering – Node.js applications simply output the data in chunks and never buffer any data.

Why Node.js is single threaded?

Node is entirely based on a single thread. It processes all requests on a single thread. But it’s just a part of the theory behind Node.js design. In fact, more than the single thread mechanism, it makes use of events and callbacks to handle a large no. of requests asynchronously.
Moreover, Node.js has an optimized design that utilizes both JavaScript and C++ to guarantee maximum performance. JavaScript executes at the server-side by Google Chrome v8 engine. And the C++ lib UV library takes care of the non-sequential I/O via background workers.
To explain it practically, let’s assume there are 100s of requests lined up in Node.js queue. As per design, the main thread of Node.js event loop will receive all of them and forwards them to background workers for execution. Once the workers finish processing requests, the registered callbacks get notified on the event loop thread to pass the result back to the user.

How do Node.js works?

A Node.js application creates a single thread on its invocation. Whenever Node.js receives a request, it first completes its processing before moving on to the next request.

Node.js works asynchronously by using the event loop and callback functions, to handle multiple requests coming in parallel. An Event Loop is a functionality which handles and processes all your external events and just converts them to a callback function. It invokes all the event handlers at a proper time. Thus, lots of work is done on the back-end, while processing a single request, so that the new incoming request doesn’t have to wait if the processing is not complete.

While processing a request, Node.js attaches a callback function to it and moves it to the back-end. Now, whenever its response is ready, an event is called which triggers the associated callback function to send this response.

Let’s Take An Example Of A Grocery Delivery.
Usually, the delivery boy goes to each and every house to deliver the packet. Node.js works in the same way and processes one request at a time. The problem arises when any one house is not open. The delivery boy can’t stop at one house and wait till it gets opened up. What he will do next, is to call the owner and ask him to call when the house is open. Meanwhile, he is going to other places for delivery. Node.js works in the same way. It doesn’t wait for the processing of the request to complete (house is open). Instead, it attaches a callback function (call from the owner of the house) to it. Whenever the processing of a request completes (the house is open), an event gets called, which triggers the associated callback function to send the response.

To summarize, Node.js does not process the requests in parallel. Instead, all the back-end processes like, I/O operations, heavy computation tasks, that take a lot of time to execute, run in parallel with other requests.

Where Node.js can be used?

When Should We Use Node.Js?
Node.js is ideal for developing streaming or event-based real-time applications that require less CPU usage such as.

  • Chat applications.
  • Game servers.

Node.js is good for fast and high-performance servers, that face the need to handle thousands of user requests simultaneously.

Good For A Collaborative Environment. It is suitable for environments where multiple people work together. For example, they post their documents, modify them by doing check-out and check-in of these documents.

Node.js supports such situations by creating an event loop for every change made to the document. The “Event loop” feature of Node.js enables it to handle multiple events simultaneously without getting blocked.

Streaming Servers. Another ideal scenario to use Node.js is for multimedia streaming servers where clients fire request’s towards the server to download different multimedia contents from it.

To summarize, it’s good to use Node.js, when you need high levels of concurrency but less amount of dedicated CPU time.

Last but not the least, since Node.js uses JavaScript internally, so it fits best for building client-side applications that also use JavaScript.

When To Not Use Node.Js?

However, we can use Node.js for a variety of applications. But it is a single threaded framework, so we should not use it for cases where the application requires long processing time. If the server is doing some calculation, it won’t be able to process any other requests. Hence, Node.js is best when processing needs less dedicated CPU time.

What do you mean by the term I/O ?

What is the difference between Asynchronous and Non-blocking?

Further details: https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/

AsynchronousNon-blocking
Asynchronous means not synchronous. Using these we can make asynchronous HTTP requests that do not wait for the server to respond. These functions continue to respond to the request for which it has already received the server response.Non-blocking functions are used in regards with I/O operations. They immediately respond with whatever data is available and keeps on running as per the requests. In case, any answer couldn’t be retrieved then the API returns immediately with an error.
  
  
  

What is package.json?

  • It is a plain JSON (JavaScript Object Notation) text file which contains all metadata information about Node.js Project or application.
  • This file should be present in the root directory of every Node.js Package or Module to describe its metadata in JSON format.
  • The file is named as “package” because Node.js platform treats every feature as a separate component. Node.js calls these as Package or Module.
  •  
Example of package.json

NPM (Node Package Manager) uses <package.json> file. It includes details of the Node.js application or package. This file contains a no. of different directives or elements. These directives guide NPM, about how to handle a module or package.

What do you understand by Event-driven programming?

Event-driven programming is a programming approach that heavily makes use of events for triggering various functions. An event can be anything like a mouse click, key press, etc. When an event occurs, a call back function is executed that is already registered with the element. This approach mainly follows the publish-subscribe pattern. Because of event-driven programming, Node.js is faster when compared to other technologies.

What is an Event loop in Node.js and how does it work?

An event loop in Node.js handles all the asynchronous callbacks in an application. It is one of the most important aspects of Node.js and the reason behind Node.js have non-blocking I/O. Since Node.js is an event-driven language, you can easily attach a listener to an event and then when the event occurs the callback will be executed by the specific listener. Whenever functions like setTimeout, http.get, and fs.readFile are called, Node.js executed the event loop and then proceeds with the further code without waiting for the output. Once the entire operation is finished, Node.js receives the output and then executes the callback function. This is why all the callback functions are placed in a queue in a loop. Once the response is received, they are executed one by one.

Explain REPL in the context of Node.js.

REPL in Node.js stands for Read, Eval, Print, and Loop. It represents a computer environment such as a window console or Unix/Linux shell where any command can be entered and then the system can respond with an output. Node.js comes bundled with a REPL environment by default. REPL can perform the below-listed tasks:

  • Read: Reads the user’s input, parses it into JavaScript data-structure and then stores it in the memory.
  • Eval: Receives and evaluates the data structure.
  • Print: Prints the final result.
  • Loop: Loops the provided command until CTRL+C is pressed twice.

List down the tasks which should be done asynchronously using the event loop

List down the steps using which “Control Flow” controls the function calls in Node.js

What Is Callback In Node.Js?

We may call “callback” as an asynchronous equivalent for a function. Node.js makes heavy use of callbacks and triggers it at the completion of a given task. All the APIs of Node.js are written in such a way that they support callbacks.

For example, suppose we have a function to read a file, as soon as it starts reading the file, Node.js return the control immediately to the execution environment so that the next instruction can be executed. Once file read operation is complete, it will call the callback function and pass the contents of the file as its arguments. Hence, there is no blocking or wait, due to File I/O. This functionality makes Node.js as highly scalable, using it processes a high number of requests without waiting for any function to return the expected result.

Further details:

  1. What are callbacks?
  2. Callback Functions in NodeJS

Moderate and advanced level:

What do you understand by callback hell?

How can you avoid callback hells?

What Is EventEmitter In Node.Js?

What’s your favourite HTTP framework and why?

What’s a test pyramid? How can you implement it when talking about HTTP APIs?

A test pyramid describes the ratio of how many unit tests, integration tests and end-to-end test you should write.

Essentially, the test pyramid describes that you should write unit tests, integration tests and end-to-end tests as well.

Further Details: Test pyramid

How can you listen on port 80 with Node?

Explain libuv.

How does Node.js handle the child threads?

What do you understand by an Event Emitter in Node.js

Performance Tips for Node.js Applications

Read here: Click here

Last updated on: April 04, 2020 4:42am