Node.js Interview Questions and Answers


What is Nodejs

Nodejs is a platform for building fast, scalable network application. Its advantages over other server side languages is that it uses event-driven, non-blocking I/O model that makes it light-weight and efficient.

Where can we use node.js?

Node.js shines in real-time web applications employing push technology over web sockets. It can be used for the following purposes:

  • Web applications (especially real-time web apps)
  • Network applications
  • Distributed systems d) General purpose applications

Can you explain how Nodejs works

It uses Google V8 Javascript engine to execute code. It contains built-in asynchronous I/O library for file, socket and HTTP communication. Node.js encapsulates libuv to handle asynchronous events.

Is Nodejs really Single-Threaded

Node.js operates on single-thread, but using non-blocking I/O calls allows it to support many concurrent connections. That means node doen’t process the requests in parallel but all the back-end stuffs which actually takes lot of time run in parallel.

For async processing, Node.js was created explicitly as an experiment. It is believed that more performance and scalability can be achieved by doing async processing on a single thread under typical web loads than the typical thread based implementation.

Using the event loop what are the tasks that should be done asynchronously?

  • I/O operations
  • Heavy computation
  • Anything requiring blocking

What is an event loop in Node.js

To process and handle external events and to convert them into callback invocations an event loop is used. So, at I/O calls, node.js can switch from one request to another .

Can you explain the Asynchronous approach in Nodejs

Nodejs operats asynchronously using event loop and callback functions.An Event Loop is a functionality which handles and processes all your external events and just converts them to a callback function. It invokes all your event handlers at a proper time. So, that means while executing a single request, it does a lot of things in the back-end so that the current request or the coming request doesn’t take much time.

Can you explain what is Globals in Nodejs

Global, Process and Buffer are combinedly termed as Globals.

Global : Its a global namespace object
Process : Its also a global object but it provides essential functionality to transform a synchronous function into a asynchronous callback.
Buffer : Raw data is stored in instances of the Buffer class.

What is the Use of underscore in Nodejs?

To access the last expression, we have to use the (_) underscore/underline character.

Mention the steps by which you can async in Node.js?

By following steps you can async Node.js

  • First class functions
  • Function composition
  • Callback Counters
  • Event loops

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.

What is EventEmitter

event.EventEmitter is a class which is used to provide a consistent interface for emitting (triggering) and binding callbacks to events. It is used internally in many of the Node core libraries and provides a solid foundation to build event-based classes and applications.

Can you create Http Server in Nodejs, explain with code

Yes, we can create Http Server in Nodejs. We can use http-server command to do so. Code:

var http = require(‘http’);
var requestListener = function (request, response) {
    response.writeHead(200, {‘Content-Type’: ‘text/plain’});
    response.end(‘Hello You\n’);
var server = http.createServer(requestListener);
server.listen(8080); // The port where you want to start with.

How Node.js overcomes the problem of blocking of I/O operations?

Node.js solves this problem by putting the event based model at its core, using an event loop instead of threads.

How to load HTML in Nodejs

How Node.js overcomes the problem of blocking of I/O operations?
Node.js solves this problem by putting the event based model at its core, using an event loop instead of threads.

To load HTML in Nodejs we have to change the Content-type from text/plain to text/html.

Can you explain the difference between Node.js vs Ajax*

The difference between Node.js and Ajax is that Ajax is a client side technology whereas Nodejs is server side technology. Ajax is used for updating the contents of the page without refreshing it whereas Nodejs is used for developing server software. Nodejs is executed by the server whereas Ajax is executed by the browser.

Can you explain the difference between readFile vs createReadStream in Nodejs

readFile – It will read the file completely into memory before making it available to the User.

createReadStream – It will read the file in chunks of the size which is specified before hand.

What is Buffers in node.js

Buffers in Node are a higher-performance alternative to strings. Since Buffers represent raw C memory allocation, they are more appropriate for dealing with binary data than strings.

They are allocated outside of V8, meaning that they are not managed by V8. While V8 is generally high performance, sometimes it will move data unnecessarily. Using a Buffer allows you to work around this and work with the memory more directly for higher performance. And do not have an encoding.

What is the difference between Node.js vs Ajax?

The difference between Node.js and Ajax is that, Ajax (short for Asynchronous Javascript and XML) is a client side technology, often used for updating the contents of the page without refreshing it. While,Node.js is Server Side Javascript, used for developing server software. Node.js does not execute in the browser but by the server.

What are the pros and cons of Node.js?

a) If your application does not have any CPU intensive computation, you can build it in Javascript top to bottom, even down to the database level if you use JSON storage object DB like MongoDB.
b) Crawlers receive a full-rendered HTML response, which is far more SEO friendly rather than a single page application or a websockets app run on top of Node.js.
a) Any intensive CPU computation will block node.js responsiveness, so a threaded platform is a better approach.
b) Using relational database with Node.js is considered less favorable.

Does node.js support ES6

Node.js is built against modern versions of V8.
All ECMAScript 2015 (ES6) features are split into three groups for shipping, staged, and in progress features:

  • All shipping features, which V8 considers stable, are turned on by default on Node.js and do NOT require any kind of runtime flag.
  • Staged features, which are almost-completed features that are not considered stable by the V8 team, require a runtime flag: –harmony.
  • In progress features can be activated individually by their respective harmony flag, although this is highly discouraged unless for testing purposes. Note: these flags are exposed by V8 and will potentially change without any deprecation notice.

You may list all the in progress features available on each Node.js release by grepping through the –v8-options argument

node --v8-options | grep "in progress"