Learn Node JS
Node.js introduction
- What Is Node.js?ode.js is an extremely powerful framework developed on Chrome’s V8 JavaScript engine that compiles the JavaScript directly into the native machine code. It is a lightweight framework used for creating server-side web applications and extends JavaScript API to offer usual server-side functionalities. It is generally used for large-scale application development, especially for video streaming sites, single page application, and other web applications.
- 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.
- 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.
- Where Node.js can be used?Node.js can be used to develop: Real-Time Web Applications Network Applications Distributed Systems General Purpose Applications
- How many types of API functions are there in Node.js?There are two types of API functions in Node.js: Asynchronous, non-blocking functions Synchronous, blocking functions
- What is package.json?The package.json file in Node.js is the heart of the entire application. It is basically the manifest file that contains the metadata of the project where we define the properties of a package.
- Explain the purpose of module.exports?A module in Node.js is used to encapsulate all the related codes into a single unit of code which can be interpreted by shifting all related functions into a single file. For example, suppose you have a file called greet.js that contains the two functions as shown below: module.exports = { greetInHindi: function(){ return "NAMASTE"; }, greetInKorean: function(){ return "ANNYEONGHASEYO"; }}; As you can see module.exports provide two functions which can be imported in another file using below code: var eduGreets = require ("./greet.js"); eduGreets.greetInHindi() //NAMASTE eduGreets.greetInKorean() //ANNYEONGHASEYO
- Differentiate between spawn() and fork() methods in Node.js?In Node.js, the spawn() is used to launch a new process with the provided set of commands. This method doesn’t create a new V8 instance and just one copy of the node module is active on the processor. When your child process returns a large amount of data to the Node you can invoke this method. Syntax: child_process.spawn(command[, args][, options]) Whereas, the fork() in Node.js is a special instance of spawn() that executes a new instance of the V8 engine. This method simply means that multiple workers are running on a single Node code base for various tasks. Syntax: child_process.fork(modulePath[, args][, options]) In case you are facing any challenges with these Node.js Interview Questions, please mention your problems in the section comment section below.
- List down the various timing features of Node.js.Node.js provides a Timers module that contains various functions for executing the code after a specified period of time. Below I have listed down the various functions provided by this module: setTimeout/clearTimeout – Used to schedule code execution after a designated amount of milliseconds setInterval/clearInterval – Used to execute a block of code multiple times setImmediate/clear immediate – Used to execute code at the end of the current event loop cycle process.nextTick – Used to schedule a callback function that needs to be invoked in the next iteration of the Event Loop
- 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.
- What do you understand by callback hell?Callback Hell is also known as the Pyramid of Doom. It is a pattern caused by intensively nested callbacks which are unreadable and unwieldy. It typically contains multiple nested callback functions which in turn make the code hard to read and debug. It is caused by improper implementation of the asynchronous logic. async_A(function(){ async_B(function(){ async_C(function(){ async_D(function(){ .... }); }); }); }); In case you are facing any challenges with these Node.js Interview Questions, please mention your problems in the section comment section below.
- What does it mean “non-blocking” in node.js?In node.js “non-blocking” means that its IO is non-blocking. Node uses “libuv” to handle its IO in a platform-agnostic way. On windows, it uses completion ports for unix it uses epoll or kqueue etc. So, it makes a non-blocking request and upon a request, it queues it within the event loop which call the JavaScript ‘callback’ on the main JavaScript thread.
- What is ‘Callback’ in node.js?Callback function is used in node.js to deal with multiple requests made to the server. Like if you have a large file which is going to take a long time for a server to read and if you don’t want a server to get engage in reading that large file while dealing with other requests, call back function is used. Call back function allows the server to deal with pending request first and call a function when it is finished.
4iv>