Posts

Showing posts from July, 2020

what is callback hell ?

https://www.geeksforgeeks.org/what-is-callback-hell-in-node-js/

what is async parallel, series, waterfall, queue, priority queue

https://medium.com/velotio-perspectives/understanding-node-js-async-flows-parallel-serial-waterfall-and-queues-6f9c4badbc17

Differentiate between spawn() and fork() methods in Node.js?

The  child_process.spawn()  method spawns the child process asynchronously, without blocking the Node.js event loop. child_process.fork() : spawns a new Node.js process and invokes a specified module with an IPC communication channel established that allows sending messages between parent and child.

What do you understand by global objects in Node.js?

In Node.js, Globals are the objects which are global in nature and are available in all the modules of the application. You can use these objects directly in your application, rather than having to include them explicitly. The global objects can be modules, functions, strings, object, etc. Moreover, some of these objects can be in the module scope instead of global scope.

Explain the concept of stub in Node.js.

In Node.js, stubs are basically the programs or functions that are used for stimulating the module or component behavior. During any test cases, stubs provide the canned answers of the functions.

How assert works in Node.js?

In Node.js, assert is used to write tests.  It only provides feedback only when any of the running test cases fails. This module gives you a set of assertion tests which are then used for testing invariants. It is basically used internally by Node.js but using require(‘assert’) code, it can be used in other applications as well. 1 2 3 4 5 6 var assert = require( 'assert' ); function mul(a, b) { return a * b; } var result = mul( 1 , 2 ); assert ( result === 2 , 'one multiplied by two is two' );

Define the concept of the test pyramid. Explain the process to implement them in terms of HTTP APIs.

According to test  pyramid  you should have a higher number of low- as compared to high-level end-to-end  tests that  running through a GUI. In terms of HTTP APIs it may be defined as: A higher number of low-level unit tests for each model Lesser integration tests to test model interactions Lesser acceptance tests for testing actual HTTP endpoints

Explain the purpose of ExpressJS package?

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Express is a light-weight web application framework to help organize our web application into a MVC architecture on the server side. We can use a variety of choices for your templating language like EJS, Jade. Express.js basically helps you manage everything, from routes, to handling requests, response and views. An example of an Express.js routing is as follow: var express = require ( 'express' ) var app = express() // respond with "hello world" when a GET request is made to the homepage app.get( '/' , function ( req, res ) { res.send( 'hello world' ) }) Express.js is a framework built on top of Node.js that facilitates the management of the flow of data between server and routes in the server-side applications. It is a lightweight and flexible framework that provides a wide range of features required for t...

Differentiate between process.nextTick() and setImmediate()?

In Node.js, process.nextTick() and setImmediate(), both are functions of the Timers module which help in executing the code after a predefined period of time. But these functions differ in their execution. The process.nextTick function waits for the execution of action till the next pass around in the event loop or once the event loop is completed only then it will invoke the callback function. On the other hand, setImmediate() is used to execute a callback method on the next cycle of the event loop which eventually returns it to the event loop in order to execute the I/O operations.

Explain the usage of a buffer class in Node.js?

Buffer class in Node.js is used for storing the raw data in a similar manner of an array of integers. But it corresponds to a raw memory allocation that is located outside the V8 heap. It is a global class that is easily accessible can be accessed in an application without importing a buffer module. Buffer class is used because pure JavaScript is not compatible with binary data. 

How does Node.js handle the child threads?

In general, Node.js is a single-threaded process and doesn’t expose the child threads or thread management methods. But you can still make use of the child threads using spawn() for some specific asynchronous I/O tasks that execute in the background and don’t usually execute any JS code or hinder 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.

Explain stream in Node.js along with its various types.

Image
Streams are pipes that let you easily read data from a source and pipe it to a destination. Simply put, a stream is nothing but an  EventEmitter  and implements some specials methods. Depending on the methods implemented, a stream becomes Readable, Writable, or Duplex (both readable and writable). For example, if we want to read data from a file, the best way to do it from a stream is to listen to data event and attach a callback. When a chunk of data is available, the readable stream emits a data event and your callback executes. Take a look at the following snippet: Types of streams are: Readable, Writable, Duplex and Transform. Streams in Node.js are the collection of data similar to arrays and strings. They are objects using which you can read data from a source or write data to a destination in a continuous manner. It might not be available at once and need not to have fit in the memory. These streams are especially useful for reading and processing a large set of data. I...

Explain the concept of Punycode in Node.js?

In Node.js, Punycode is an encoding syntax that is used for converting Unicode (UTF-8) string of characters into a basic ASCII string of characters.

How do Node.js works?

Node.js is a virtual machine that uses JavaScript as its scripting language and runs on a v8 environment.  It works on a single-threaded event loop and a non-blocking I/O which provides high rate as it can handle a higher number of concurrent requests.  Also, by making use of the  ‘HTTP’ module , Node.js can run on any stand-alone web server. 

Why Node.js is single threaded?

Node.js uses a single-threaded model in order to support async processing.  With async processing, an application can perform better and is more scalable under web loads.  Thus, Node.js makes use of a  single-threaded model  approach rather than typical thread-based implementation.