Node JS Interview Questions

Node JS interview questions

The interview is a critical part of your life as what you say in an interview matters the most and helps you in deciding your career in the future stage. If you are worried about cracking the interview as don’t know what type of questions the interviewer will ask you, then you can blindly follow our Node JS Interview questions to understand the pattern. Our team of experts and professionals ensure to create questions and answers that are usually asked in the interview with the candidates.

About Node JS

Node.JS is a cross-platform JavaScript environment that implements all the JavaScript codes outside the browser that you are using. It is an open source platform when you can write a command using the command lines tools and applications in order to run the scripts and create a dynamic web page based on the requirements of the clients. You can create contents that are not similar to the others using a single programming language and not different languages. Considered as one of the best platforms to develop a webpage, there are many organizations who are focusing onboarding this platform in their operations division.

Best Node JS Interview Questions of 2020

1. What is Node js ?

Node Js is one of the most popular and powerful server technologies today. It allows you built the entire website only in one programming Language i.e Javascript. Node js is free and open source server technology that uses Javascript to create complete web software.It runs on various platforms like Windows, Linux, Unix, Mac OS X, etc.

2. Explain Modules in Node Js ?

Modules are reusable block of code whose existence does not impact other code in any way. It is not supported by Javascript. Modules are introduced in ES6. Modules are important for Maintainability, Reusability, and Namespacing of Code.

3. Is Node Js Single-threaded ?

YYes, Node Js is single threaded to perform asynchronous processing. Doing async processing on a single thread could provide more performance and scalability under typical web loads than the typical thread-based implementation.

4. For what require() is used in Node Js ?

require() is used to include modules from external files in Node Js. It is the easiest way to include a module in Node. Basically require is a function that takes a string parameter which contains the location of the file that you want to include. It reads the entire javascript file, executes the file, and then proceeds to return the exports object.
Syntax:

require('path');

5. In which Language Node Js is written ?

Node js is written in C, C++,JavaScript.It uses Google’s open source V8 Javascript Engine to convert Javascript code to C++.

6. List the types of application you can build using Node Js?

Using Node Js you can build applications like:

  • Internet of Things
  • Real-Time Chats Applications
  • Complex Single-Page Applications
  • Real-Time Collaboration Tools
  • Streaming apps
  • Microservices / API’s

7. Explain what is libuv in Node Js ?

ibuv is Cross-platform I/O abstraction library that supports asynchronous I/O based on event loops.It is written in C and released under MIT Licence.

libuv support Windows IOCP, epoll(4), kqueue(2), and Solaris event ports. Initially, it was designed for Node.js but later it is also used by other software projects.

Reference : https://en.wikipedia.org/wiki/Libuv

8. Why Zlib is used in Node js ?

Zlib is Cross-platform data compression library. It was written by Jean-loup Gailly and Mark Adler. In Node js, you can Zlib for Threadpool, HTTP requests, and responses compression and Memory Usage Tuning. In order to use zlib in node js, you need to install node-zlib package. After installation below is sample code to use Zlib.

var Buffer = require('buffer').Buffer;
var zlib = require('zlib');

var input = new Buffer('lorem ipsum dolor sit amet');
var compressed = zlib.deflate(input);
var output = zlib.inflate(compressed);

Further Reading https://nodejs.org/api/zlib.html

https://en.wikipedia.org/wiki/Zlib

9. How Node js read the content of a file?

Normally NodeJs reads the content of a file in non-blocking, asynchronous way. Node Js uses its fs core API to deal with files. The easiest way to read the entire content of a file in nodeJs is with fs.readFile method. Below is sample code to read a file in NodeJs asynchronously and synchronously.

Reading a file in node asynchronously/ non-blocking

var fs = require('fs'); 
fs.readFile('DATA', 'utf8', function(err, contents) {
    console.log(contents);
});
console.log('after calling readFile');

Reading a file in node asynchronously/blocking

var fs = require('fs'); 
var contents = fs.readFileSync('DATA', 'utf8');
console.log(contents);

10. What are Streams? List types of streams available in Node Js ?

Streams are special types of objects in Node that allow us to read data from a source or write data to a destination continuously. There are 4 types of streams available in Node Js, they are

  • Readable − For reading operation.
  • Writable − For writing operation.
  • Duplex − Used for both read and write operation.
  • Transform − A type of duplex stream where the output is computed based on the input.

11. What's the first argument passed to a Node Js callback handler?

In Node Js all core modules, as well as most of the community-published modules, follows a pattern where the first argument to any callback handler is an error object. this object is optional, if there is no error then in that case null or undefined is passed to the callback.

Example of the callback function

function callback(err, results) {
	// usually we'll check for the error before handling results
	if(err) {
		// handle error somehow and return
	}
		// no error, perform standard callback handling
}

12. Explain the difference between readFile and createReadStream in Node js?

  • readFile load the whole file which you had marked to read whereas createReadStream reads the complete file in the parts of the size you have declared.
  • The client will receive the data faster in the case of createReadStream in contrast with readFile.
  • In readFile, a file will first completely read by memory and then transfers to a client but in later option, a file will be read by memory in a part which is sent to clients and the process continue until all the parts finish.

13. What is JIT and how is it related to Node JS?   

JIT stands for Just-in-time. A JIT compiler is a program which is used to send bytecode (it consists of instruction that can be interpreted) to the processor by converting it into instruction. After you have done with writing a program, the compiler compiles the source language statements into bytecode instead of compiling it into the code that carries the information which is similar to the specific hardware platform's processor.

Relation of JIT with Node: Virtual machine of Nodejs has JIT compilation which improves the execution speed of the code. The virtual machine takes the source code and converts to machine code in runtime. By this, the hot functions which are called very often are compiled to machine code and, hence increasing speed.

14. How to create a simple server in Node js that returns Hello World?

By writing following line of code, you can create a server in Node Js.

var http =require('http');
http.createServer(function(req,res){

res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello World\n');

}).listen(1320,'127.0.0.3');

15. what is Closures?

A Closure is a function defined within another scope that has access to all the variables within the outer scope.

Global variables can be made local (private) with closures.

16. How to use aggregation in Mongoose?

Aggregations are a set of functions that are used to manipulate the data that has been returned from a MongoDB query. In Mongoose, aggregations work as a pipeline. The aggregate function accepts the array of data transformations which are applied by data using different methods in terms of arguments.

Syntax: db.customers.aggregate([ ... aggregation steps go here ...]);

In above, aggregation is applied to data of customers.

17. What is difference between put and patch?

The main difference between the put and the patch is

Put Patch
The embedded entity is believed to the modified version of the resources that are deposited on the original server. It is requested to the client to replace the stored is substituted. In this, the information regarding the way of modifying the original server which has the resources to produce a new version is found.
At the time of updating the resource, you need to forward full payload as the request. At the time of updating the resource, you only need to send the parameter of the resource which you want to update.

18. List types of Http requests?

Http defines a set of request methods to perform the desired actions. These request methods are:

  1. GET: The GET method asked for the representation of the specifies resource. This request used to retrieve the data.
  2. POST: The POST technique is utilized to present an element to the predetermined resource, generally causing a change in state or reactions on the server.
  3. HEAD: The HEAD method is similar to the GET method but asks for the response without the response body.
  4. PUT: This method is used to substitute all current representations with the payload.
  5. DELETE: It is used to delete the predetermined resource.
  6. CONNECT: This request is used to settle the TCP/IP tunnel to the server by the target resource
  7. OPTION: This method is used to give back the HTTP strategies to communicate with the target resource.
  8. TRACE: This method echoes the message which tells the customer how much progressions have been made by an intermediate server.
  9. PATCH: The PATCH method gives partial modifications to a resource.

19. What is the revealing module pattern?

Revealing module pattern is similar to Module Pattern.IT ExposES only the properties and methods you want via an returned Object.

var greeting = 'Hello world'
function greet() {
 console.log(greeting)
}
module.exports = {
  greet: greet
}

20. How will you read a file using Node?

ollowing is the syntax of one of the methods to read from a file:

fs.read(fd, buffer, offset, length, position, callback)

This method will use file descriptor to read the file, if you want to read file using file name directly then you should use another method available.

Parameters

Here is the description of the parameters used −

  • fd − This is the file descriptor returned by file fs.open() method.

  • buffer − This is the buffer that the data will be written to.

  • offset − This is the offset in the buffer to start writing at.

  • length − This is an integer specifying the number of bytes to read.

  • position − This is an integer specifying where to begin reading from in the file. If position is null, data will be read from the current file position.

  • callback − This is the callback function which gets the three arguments, (err, bytesRead, buffer).

21. How will you write a file using Node?

Following is the syntax of one of the methods to write into a file:

fs.writeFile(filename, data[, options], callback)

This method will over-write the file if file already exists. If you want to write into an existing file then you should use another method available.

Parameters

Here is the description of the parameters used:

  • path − This is string having file name including path.

  • data − This is the String or Buffer to be written into the file.

  • options − The third parameter is an object which will hold {encoding, mode, flag}. By default encoding is utf8, mode is octal value 0666 and flag is 'w'

  • callback − This is the callback function which gets a single parameter err and used to to return error in case of any writing error.

22. What is the difference between installing and activating a theme?

When a theme is activated it’s what’s controlling your site, while an installed theme is simply part of your theme library and available to activate.

23. How will you close a file using Node?

Following is the syntax of one of the methods to close an opened file:

fs.close(fd, callback)

Parameters

Here is the description of the parameters used:

  • fd − This is the file descriptor returned by file fs.open() method.

  • callback − This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

24. How will you get information about a file using Node?

Following is the syntax of the method to get the information about a file:

fs.stat(path, callback)

Parameters

Here is the description of the parameters used:

  • path − This is string having file name including path.

  • callback − This is the callback function which gets two arguments (err, stats) where stats is an object of fs.Stats type which is printed below in the example.

25. How will you truncate a file using Node?

Following is the syntax of the method to truncate an opened file −

fs.ftruncate(fd, len, callback)

Parameters

Here is the description of the parameters used:

  • fd − This is the file descriptor returned by file fs.open() method.

  • len − This is the length of the file after which file will be truncated.

  • callback − This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

26. How will you delete a file using Node?

Following is the syntax of the method to delete a file −

fs.unlink(path, callback)

Parameters

Here is the description of the parameters used:

  • path − This is the file name including path.

  • callback − This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

27. How will you create a directory?

Following is the syntax of the method to create a directory:

fs.mkdir(path[, mode], callback)

Parameters

Here is the description of the parameters used:

  • path − This is the directory name including path.

  • mode − This is the directory permission to be set. Defaults to 0777.

  • callback − This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

28. How will you delete a directory?

Following is the syntax of the method to remove a directory:

fs.rmdir(path, callback)

Parameters

Here is the description of the parameters used:

  • path − This is the directory name including path.

  • callback − This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

29. How will you read a directory?

Following is the syntax of the method to read a directory:

fs.readdir(path, callback)

Parameters

Here is the description of the parameters used:

  • path − This is the directory name including path.

  • callback − This is the callback function which gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.

30. What is the purpose of __filename variable?

The __filename represents the filename of the code being executed. This is the resolved absolute path of this code file. For a main program this is not necessarily the same filename used in the command line. The value inside a module is the path to that module file.

Career scopes and salary scale

As the market of technology is growing every day, the career opportunities and scopes in this specific field are also increasing day by day. With the platform to write commands in a single programming language, it has become easier for the companies to hire developers who have the basic knowledge and expertise of commanding and programming language. If you have the right skills to apply for this job, then you have a chance to earn more than a lakh per month based on your performance in the job. For a fresher, he or she is more likely to earn about 90,000 to 100,000 dollars per annum, while for an experienced candidate, the salary increases up to 180,000 to 200,000 dollars based on his or her skill set.

Conclusion

Node JS is gaining a lot of attention in the market because of its demands across all the industries who work in the technological sector. If you want to crack the Node JS interview questions, just understand and learn the answers that our experts have provided.

Intellinuts Terms : A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z