Ques: 1. What can be the possible use of DNS module in Node.js?
Ans. This is one of the most asked Node js interview questions. The DNS module consists of an asynchronous network wrapper. Let’s have a look at the most commonly used functions of this module:
1. DNS.lookup(address, options, callback) – This method takes any website’s address as its first parameter and returns the corresponding first IPV4 or IPV6 record. The options parameter can be an integer or object. If no options are provided both IPV4 and IPV6 are valid inputs. The third parameter is the callback functions.
2. DNS.lookupservice(address, port, callback) – This function converts any physical address to an array of record types. The record types are specified by the second parameter, rrbyte. The third method is the callback function.
3. dns.getServers() – This function returns an array of IP address strings that are currently configured for DNS resolution, formatted according to rfc5952. A string will include a port section if a custom port is used.
4. DNS.setServers() – This function sets the IP address and port of servers to be used when performing DNS resolution. The DNS.setServers() method must not be called while a DNS query is in progress.
Ques: 2. How can you use Timers in Node.js?
The functions provided by Node.js for Timers are:
setTimeout(), setImmediate(), and setInterval.
Ques: 3. What do you mean by non-blocking mean in Node.js?
Ques: 4. What is the main difference between Node.js and Ajax?
Ques: 5. What are the various pros and cons of Node.js?
Ans. Let’s take a look at the most important pros and cons:
- Crawlers receive a full-rendered HTML response which is great for SEO.
- Node.js responsiveness is blocked by an intensive CPU computation so a threaded platform would a better approach in those cases.
- Using a relational database with Node.js is considered less favourable.
Ques: 6. What is the use of method spawn() and fork()?
Ans. The spawn method is used when a new process is to be launched with a given set of commands. Check out the following command:
child_process.spawn(command[, args][, options])
The fork method is considered to be a special case for spawn() method. The following code shows how to use it:
child_process.fork(modulePath[, args][, options])
Ques: 7. Can you explain the EventEmitter in Node.js?
Ans. EventMitter class for event module helps with raising and handling custom events. You can access it with the following code:
// Import events module
var events = require(‘events’);
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();
Ques: 8. What do you understand by tracing?
Ans. The purpose of tracing is to trace information generated by V8. To enable it, pass flag-trace-events-enabled when starting the node.
All recorded categories can be specified by the flag–trace-event categories. The enabled logs can be opened as chrome://tracing in Chrome.
Ques: 9. Can you explain the control flow function and steps to execute it?
Ans. The control flow function is the code that runs between asynchronous function calls. Following steps should be followed to execute it:
- Control the order of execution.
- Collect data.
- Limit concurrency.
- Call the next step in the program.
Ques: 10. What is the file system module of Node js?
Ans. The file system module performs a file-related operation. It comprises synchronous and asynchronous functions to read/write files.
For instance, readFile() function is asynchronous function to read file content from specified path and readFileSync() is synchronous function to read files.
Ques: 11. What are some of the most popular modules of Node.js?
Ans. The most popular modules Nodes.js are:
Ques: 12. What do you understand by package.json in node.js? What is it used for?
Ans. Package.json holds various metadata information about the project. The information contained in this file is then given to npm to identify the project and to handle its dependencies.
Some of the fields are name, description, author, and dependencies.
The dependencies are then installed whenever the project is installed through npm. Moreover, if the npm install is run in the root directory of the project, the dependencies will be installed in ./node_modules directory.
Ques: 13. What is the difference between Asynchronous and Non-blocking?
Ans. Asynchronous or simply not synchronous means that the HTTP requests are not waiting for the server response. You can continue with other block and respond to the server response when received.
Non-Blocking, on the other hand, is a term commonly used with IO. For instance, non-blocking read/write calls return and expect the caller to call again. Read will wait until it has some data and put calling thread to sleep.
Ques: 14. What are the features of Node.js?
This system uses a single threaded model with event looping. With the event mechanism helping the server respond in a non-blocking way, the server is highly scalable compared to the other servers that create limited threads to handle requests.
Ques: 15. Why should you use Node.js?
Ans. One could use Node.js when they want to easily build scalable apps. The biggest pros include:
- It is very fast in code execution.
- It is asynchronous and event-driven.
- It is single-threaded but highly scalable.
- No buffering.
Ques: 16. Explain the role of REPL in Node.js.?
Ques: 17. What is libuv library? And what is its use in Node.js?
Ans. Libuv is a multi-platform support library with a focus on asynchronous I/O. Although it was primarily developed for Node.js, it’s also commonly used by other systems such as Luvit, Julia, pyuv etc.
Back in the days when the whole Node project started, it was using Google’s V8 and Marc Lehmann’s libev. However, the problem with libev was that it ran only on Unix so they had to come up with the new solution to make it work on Windows, especially once node.js grew in popularity.
Libuv was an abstraction around libev or IOCP depending on the platform, providing users an API based on libev. In the node-v0.9.0 version of libuv, libev was removed.
Here are some of the key features:
- Full-featured event loop backed by epoll, kqueue, IOCP, event ports.
- Asynchronous TCP and UDP sockets.
- Asynchronous file and file system operations.
- Child processes.
- File system events.
Ques: 18. What are the two arguments that async.queue takes?
Ans. These two arguments are:
a) Task function
b) Concurrency value
Ques: 19. How to avoid Callback Hell?
A. Basically, every time a long-running query finishes its execution, the callback associated with the query is run. Since Node.js uses single thread only, it happens that it leads to numerous queued events. And that is where the callback comes in.
There are 4 common solutions for this issue:
Modular code – the code split into smaller modules that are later joined together to the main module again
Promise mechanism – ensures either a result or error; it’s an alternative way for the async code and it takes two optional arguments, one of which is called depending on the state of promise
Use of generators – they wait and resume using the yield keyword but can also suspend and resume async operations.
Async mechanism – the module with <async.waterfall> API which passes data from one operation to another using the next callback.
Ques: 20. What is NPM?
A. NPM stands for Node Package Manager. It has 2 important functions:
It works on Online Repository for node.ls packages which are present at <nodejs.org>. In addition to that, it also works as a command line utility and does version management.
You can verify version using below command: npm –version.
To install any module you can use: npm install <Module Name>