Sunday, October 21, 2018

Advance Node.JS Interview Questions And Answers

Now a days NodeJs is very popular in the industry. If you're looking for Node.JS Interview Questions for Experienced or Beginners, you are at right place. There are lot of opportunities from many reputed companies in the world. According to research Node.JS has a market share of about 2.6%. So, You still have opportunity to move ahead in your career in Node.JS Development. Techbowl arranged Advanced Node JS Interview Questions 2018 or node js programming interview questions that helps you in cracking your interview & acquire dream career as Node.JS Developer.

Advance Node.JS Interview Questions And Answers

Advance Node.JS Interview Questions And Answers

Q1: What is Node.js?

Node.js is a web application framework built on Google Chrome's JavaScript Engine (V8 Engine).

Node.js comes with runtime environment on which a Javascript based script can be interpreted and executed (It is analogus to JVM to JAVA byte code). This runtime allows to execute a JavaScript code on any machine outside a browser. Because of this runtime of Node.js, JavaScript is now can be executed on server as well.

Node.js = Runtime Environment + JavaScript Library

Q2: What is an error-first callback?

Error-first callbacks are used to pass errors and data. The first argument is always an error object that the programmer has to check if something went wrong. Additional arguments are used to pass data.

[code] fs.readFile(filePath, function(err, data) { if (err) { //handle the error } // use the data object }); [/code]

Q3: What are the benefits of using Node.js?

Following are main benefits of using Node.js

  • Aynchronous and Event Driven- All APIs of Node.js library are aynchronous that is non-blocking. It essentially means a Node.js based server never waits for a API to return data. Server moves to next API after calling it and a notification mechanism of Events of Node.js helps server to get response from the previous API call.
  • Very Fast- Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution.
  • Single Threaded but highly Scalable - Node.js uses a single threaded model with event looping. Event mechanism helps server to respond in a non-bloking ways and makes server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and same program can services much larger number of requests than traditional server like Apache HTTP Server.
  • No Buffering - Node.js applications never buffer any data. These applications simply output the data in chunks.

Q4: If Node.js is single threaded then how it handles concurrency?

Node provides a single thread to programmers so that code can be written easily and without bottleneck. Node internally uses multiple POSIX threads for various I/O operations such as File, DNS, Network calls etc.

When Node gets I/O request it creates or uses a thread to perform that I/O operation and once the operation is done, it pushes the result to the event queue. On each such event, event loop runs and checks the queue and if the execution stack of Node is empty then it adds the queue result to execution stack.

This is how Node manages concurrency.

Q5: What is global installation of dependencies?

Globally installed packages/dependencies are stored in /npm directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but can not be imported using require() in Node application directly. To install a Node project globally use -g flag.

Q6: How can you avoid callback hells?

To do so you have more options:

  • modularization: break callbacks into independent functions
  • use Promises
  • use yield with Generators and/or Promises
Q7: What's the event loop?

The event loop is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible.

Every I/O requires a callback - once they are done they are pushed onto the event loop for execution. Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed.

Q8: How Node prevents blocking code?

By providing callback function. Callback function gets called whenever corresponding event triggered.

Q9: What is Event Emmitter?

All objects that emit events are members of EventEmitter class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object.

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously.

[code] const EventEmitter = require('events'); class MyEmitter extends EventEmitter {} const myEmitter = new MyEmitter(); myEmitter.on('event', () => { console.log('an event occurred!'); }); myEmitter.emit('event'); [/code]

Q10: What tools can be used to assure consistent code style?

You have plenty of options to do so:

These tools are really helpful when developing code in teams, to enforce a given style guide and to catch common errors using static analysis.

Q11: Provide some example of config file separation for dev and prod environments

A perfect and flawless configuration setup should ensure:
  • keys can be read from file AND from environment variable
  • secrets are kept outside committed code
  • config is hierarchical for easier findability
Consider the following config file:

 var config = {
 production: {
 mongo : { billing: '****' }
 default: {
     mongo : { billing: '****' }
 exports.get = function get(env) { return config[env] || config.default; }

And it's usage:

[code] const config = require('./config/config.js').get(process.env.NODE_ENV); const dbconn = mongoose.createConnection(config.mongo.billing); [/code]


Post a Comment