Home Interview Questions Top 32 Node.js Interview Questions And Answers With PDF Download 2019

Top 32 Node.js Interview Questions And Answers With PDF Download 2019

551
0
Top 32 Node.js Interview Questions And Answers 2019

Top 32 Node.js Interview Questions And Answers: Here are the top interview questions of Node.JS. Be prepared for the interview. You can download the entire questions in a pdf format. so guys All the best for your interview.

1) What is Node.js?

Node.js is Server-side scripting which is used to build scalable programs. It is a web application framework built on Google Chrome’s JavaScript Engine. It runs within the Node.js runtime on Mac OS, Windows, and Linux with no changes. This runtime facilitates you to execute a JavaScript code on any machine outside a browser.


2) Is Node.js free to use?

Yes. It is released under MIT license and is free to use.


3) Is Node a single threaded application?

Yes. Node is a single-threaded application with event looping.


4) What is the purpose of Node.js?

These are the following purposes of Node.js:


5) What are the advantages of Node.js?

Following are the Top 32 Node.js Interview Questions And Answers:

  • Node.js is asynchronous and event-driven. All API?s of Node.js library are non-blocking, and its server doesn’t wait for an API to return data. It moves to the next API after calling it, and a notification mechanism of Events of Node.js responds to the server from the previous API call.
  • Node.js is very fast because it builds on Google Chrome?s V8 JavaScript engine. Its library is very fast in code execution.
  • Node.js is single threaded but highly scalable.
  • Node.js provides a facility of no buffering. Its application never buffers any data. It outputs the data in chunks.

6. Explain REPL In Node.Js?

Answer.

The REPL stands for “Read Eval Print Loop”. It is a simple program that accepts the commands, evaluates them, and finally prints the results. REPL provides an environment similar to that of Unix/Linux shell or a window console, in which we can enter the command and the system, in turn, responds with the output. REPL performs the following tasks.

  • READ
    • It Reads the input from the user, parses it into JavaScript data structure and then stores it in the memory.
  • EVAL
    • It Executes the data structure.
  • PRINT
    • It Prints the result obtained after evaluating the command.
  • LOOP
    • It Loops the above command until the user presses Ctrl+C two times.

7. Is Node.Js Entirely Based On A Single-Thread?

Answer.Top 32 Node.js Interview Questions And Answers

Yes, it’s true that Node.js 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 which 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 to background workers for execution. Once the workers finish processing requests, the registered callbacks get notified on event loop thread to pass the result back to the user.

8. How To Get Post Data In Node.Js?

Answer.

Following is the code snippet to fetch Post Data using Node.js.

app.use(express.bodyParser()); 
app.post('/', function(request, response){ console.log(request.body.user); });

9. How To Make Post Request In Node.Js?

Answer.

Following code snippet can be used to make a Post Request in Node.js.

var request = require('request'); 
request.post(
'http://www.example.com/action',
{ form: { key: 'value' } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);

10. What Is Callback In Node.Js?

Answer.Top 32 Node.js Interview Questions And Answers

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.

11) How can you avoid callbacks?

To avoid callbacks, you can use any one of the following options:

  • You can use modularization. It breaks callbacks into independent functions.
  • You can use promises.
  • You can use yield with Generators and Promises.

12) Does Node.js provide Debugger?

Yes, Node.js provides a simple TCP based protocol and built-in debugging client. For debugging your JavaScript file, you can use debug argument followed by the js file name you want to debug.

Syntax:

  1. node debug [script.js | -e “script” | <host>:<port>]  

13) What is a control flow function?

Top 32 Node.js Interview Questions And Answers: Control flow function is a generic piece of code that runs in between several asynchronous function calls.


14) How “Control Flow” controls the functions calls?

The control flow does the following job:

  • Control the order of execution
  • Collect data
  • Limit concurrency
  • Call the next step in a program

15) Is it possible to access DOM in Node?

No, it is not possible to access DOM in Node.

16. How To Load HTML In Node.Js?

Answer.

To load HTML in Node.js we have to change the “Content-type” in the HTML code from text/plain to text/html.
Let’s see an example where we have created a static file in web server.

fs.readFile(filename, "binary", function(err, file) { 
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"}); response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});

Now we will modify this code to load an HTML page instead of plain text.

fs.readFile(filename, "binary", function(err, file) { 
if(err) {
response.writeHead(500, {"Content-Type": "text/html"}); response.write(err + "\n");
response.end();
return;
}
response.writeHead(200, {"Content-Type": "text/html"}); response.write(file);
response.end();
});

17. What Is EventEmitter In Node.Js?

Answer.

Events module in Node.js allows us to create and handle custom events. The Event module contains “EventEmitter” class which can be used to raise and handle custom events. It is accessible via the following code.

// Import events module 
var events = require('events');
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

When an EventEmitter instance encounters an error, it emits an “error” event. When a new listener gets added, it fires a “newListener” event and when a listener gets removed, it fires a “removeListener” event.

EventEmitter provides multiple properties like “on” and “emit”. The “on” property is used to bind a function to the event and “emit” is used to fire an event.

18. How Many Types Of Streams Are Present In Node.Js?

Answer.

Top 32 Node.js Interview Questions And Answers: Stream in Node.js are objects that allow reading data from a source or writing data to a specific destination in a continuous fashion. In Node.js, there are four types of streams.

  • <Readable> – This is the Stream to be used for reading operation.
  • <Writable> – It facilitates the write operation.
  • <Duplex> – This Stream can be used for both the read and write operations.
  • <Transform> – It is a form of a duplex Stream, which performs the computations based on the available input.

All the Streams, discussed above are an instance of an “EventEmitter” class. The event thrown by the Stream varies with time. Some of the commonly used events are as follows.

  • <data> – This event gets fired when there is data available for reading.
  • <end> – The Stream fires this event when there is no more data to read.
  • <error> – This event gets fired when there is any error in reading or writing data.
  • <finish> – It fires this event after it has flushed all the data to the underlying system.

19. List And Explain The Important REPL Commands?

Answer.

Following is the list of some of the most commonly used REPL commands.

  • <.help> – It displays help for all the commands.
  • <tab Keys> – It displays the list of all the available commands.
  • <Up/Down Keys> – Its use is to determine what command was executed in REPL previously.
  • <.save filename> – Save the current REPL session to a file.
  • <.load filename> – To Load the specified file in the current REPL session.
  • <ctrl + c> – used to Terminate the current command.
  • <ctrl + c (twice)> – To Exit from the REPL.
  • <ctrl + d> – This command perfoms Exit from the REPL.
  • <.break> – It leads Exitting from multiline expression.
  • <.clear> – Exit from multiline expression.

20. What Is NPM In Node.Js?

Answer.

NPM stands for Node Package Manager. It provides following two main functionalities.

  • It works as an Online repository for node.js packages/modules which are present at <nodejs.org>.
  • It works as Command line utility to install packages, do version management and dependency management of Node.js packages.

NPM comes bundled along with Node.js installable. We can verify its version using the following command-

$ npm --version

NPM helps to install any Node.js module using the following command.

$ npm install <Module Name>

For example, following is the command to install a famous Node.js web framework module called express-

$ npm install express

21) Does Node.js supports cryptography?

Yes, Node.js Crypto module supports cryptography. It provides cryptographic functionality that includes a set of wrappers for open SSL’s hash HMAC, cipher, decipher, sign and verify functions. For example:

  1. const crypto = require(‘crypto’);    
  2. const secret = ‘abcdefg’;    
  3. const hash = crypto.createHmac(‘sha256’, secret)    
  4.                    .update(‘Welcome to JavaTpoint’)    
  5.                    .digest(‘hex’);    
  6. console.log(hash);    

22) What is npm? What is the main functionality of npm?

npm stands for Node Package Manager. Following are the two main functionalities of npm:

  • Online repositories for node.js packages/modules which are searchable on search.nodejs.org
  • Command line utility to install packages, do version management and dependency management of Node.js packages.

23) What tools can be used to assure a consistent style in Node.js?

Following is a list of tools that can be used in developing code in teams, to enforce a given style guide and to catch common errors using static analysis.

  • JSLint
  • JSHint
  • ESLint
  • JSCS

24) What is the difference between operational and programmer errors?

Operational errors are not bugs, but create problems with the system like request timeout or hardware failure. On the other hand, programmer errors are actual bugs.


25) What is the difference between the global installation of dependencies and local installation of dependencies?

  • Global installation of dependencies is stored in /npm directory. While local installation of dependencies stores in the local mode. Here local mode refers to the package installation in node_modules directory lying in the folder where Node application is present.
  • Globally deployed packages cannot be imported using require() in Node application directly. On the other hand, locally deployed packages are accessible via require().
  • To install a Node project globally -g flag is used.
    1. C:\Nodejs_WorkSpace>npm install express ?g   
  • To install a Node project locally, the syntax is:
    1. C:\Nodejs_WorkSpace>npm install express   

26. What Is Chaining Process In Node.Js?

Answer.

It’s an approach to connect the output of one stream to the input of another stream, thus creating a chain of multiple stream operations.

27. How To Create A Custom Directive In AngularJS?

Answer.

To create a custom directive, we have to first register it with the application object by calling the <directive> function. While invoking the <register> method of <directive>, we need to give the name of the function implementing the logic for that directive.

For example, in the below code, we have created a copyright directive which returns a copyright text.

app.directive('myCopyRight', function () 
{
return
{
template: '@CopyRight MyDomain.com '
};
});

Note – A custom directive should follow the camel case format as shown above.

28. What Is A Child_process Module In Node.Js?

Answer.

Node.js supports the creation of child processes to help in parallel processing along with the event-driven model.

The Child processes always have three streams <child.stdin>, child.stdout, and child.stderr. The <stdio> stream of the parent process shares the streams of the child process.

Node.js provides a <child_process> module which supports following three methods to create a child process.

  • exec – <child_process.exec> method runs a command in a shell/console and buffers the output.
  • spawn – <child_process.spawn> launches a new process with a given command.
  • fork – <child_process.fork> is a special case of the spawn() method to create child processes.

29. What Are The Different Custom Directive Types In AngularJS?

Answer.Top 32 Node.js Interview Questions And Answers

AngularJS supports a no. of different directives which also depend on the level we want to restrict them.

So in all, there are four different kinds of custom directives.

  • Element Directives (E)
  • Attribute Directives (A)
  • CSS Class Directives (C)
  • Comment Directives (M)

30. What Is A Control Flow Function? What Are The Steps Does It Execute?

Answer.

It is a generic piece of code which runs in between several asynchronous function calls is known as control flow function.

It executes the following steps.

  • Control the order of execution.
  • Collect data.
  • Limit concurrency.
  • Call the next step in the program.

31) What is the Punycode in Node.js?

The Punycode is an encoding syntax which is used to convert Unicode (UTF-8) string of characters to ASCII string of characters. It is bundled with Node.js v0.6.2 and later versions. If you want to use it with other Node.js versions, then use npm to install Punycode module first. You have to used require (‘Punycode’) to access it.

Syntax:

  1. punycode = require(‘punycode’);  

32) What does Node.js TTY module contains?

The Node.js TTY module contains tty.ReadStream and tty.WriteStream classes. In most cases, there is no need to use this module directly. You have to used require (‘tty’) to access this module.

Syntax:

  1. var tty = require(‘tty’);   

Top 32 Node.js Interview Questions And Answers 2019 Pdf

Check out latest posts: