How to crack node js interviews
 

Node.js is a framework used for server-side scripting and runs on Google’s JavaScript engine. To know more about node.js and read essential questions on the topic, check out node js interview questions on our website.

Topics to study for node.js interview

  • JavaScript
  • Features and advantages
  • Express framework
  • NPM
  • Promise, callback, async, await
  • Variable declarations
  • Modules
  • Basic database knowledge
  • File handling
  • Clustering
  • Event handling
  • Unit testing

Sometimes, the interviewer may ask you to write two or three lines of simple code based on your understanding. In general, keep your answers precise unless you are asked for details and example codes. Check out short answers to node js questions from our website Best Interview Questions.

In this blog, we have touched upon a few common topics asked in the interviews. For details, you can also check the advanced node js interview questions on our website.

# JavaScript

It is a broad topic. To be able to score well in a node.js interview, the first thing is to be well-versed with JavaScript as the node.js library is built over JavaScript. You can also check our best JavaScript Interview Questions and Answers.

A few common topics that interviewers would like to test you on are –

  • Types of inheritance in JavaScript
  • How to use pass by value and pass by reference
  • Concept of closure
  • Writing simple code using JavaScript

# Features and advantages

This could be the ice breaker for the interview session. Most interviewers start with some easy questions to make you comfortable. To read more about the features and advantages of node js, do read node js interview questions on our website. As a starting point, you can say a few words like –

Node.js is not a language. It is a framework that enables the use of JavaScript for server-side scripting, which makes web pages more dynamic. With node.js,

  • JavaScript code is converted to native code at run-time
  • If there are more than 2 requests for I/O operations, none of them has to wait. Event looping takes care of handling concurrent requests.
  • This means a single thread can take care of more requests for saving memory.
  • Unhandled exceptions can be handled at the process level using a handler for the event “uncaughtexception.”

# Callback

Node uses callbacks a lot to make asynchronous calls possible. Callback ensures no I/O operation is blocked.

Click here to know more about callback functions.

Example –

var filesys = require(‘fs’);
   filesys.readFile('details.txt', function (err, output) {
   if (err) return console.error(err);
   console.log(output.toString());
});
console.log(‘File reading successful’);

In the above example, the readFile starts and the next code is also executed simultaneously. If callback is not used, the output would be printed only after the entire file is read.

# Promise

This is a rather interesting question asked in every node js interview. You must be thorough with the concepts of event looping and callback to answer the purpose of promise.

For an application, developers may need to nest multiple callback functions. This may get confusing at some point. The ‘promise’ module promises to avoid this problem.

Example –
Code without promise Code with promise
DatabaseInstance.connect(url, function(err, db){
   // some code
});
require(‘promise’);
DatabaseInstance.connect(url).then(function(err, db){
   // some code
});

When we add the promise module, the ‘then’ function is available so that the ‘some code’ is executed after the database connection is established.

# Await, async

Node v8 introduced await which awaits the function returning the promise. The feature needs to be declared as an async function.

To check the concept of async / await you can Click Here. You can also read our best node js interview questions and answer that will help you to crack your future interviews.

Example –

async function getResponse(req, res){
   let response = await request.get(‘url’);
   //execute some code
}

Note that this will execute request.get() and wait for completion to execute the next line of code. The code also becomes readable and easy to understand.

# let, var and const

Did you notice the ‘let’ in previous code bit, which has been used for variable declaration? The declaration is another favorite question of the interviewers, and you should know their properties well. We have all the 3 variables covered in detail in node js interview questions.

Here is a quick overview –

let – let declaration is block ({}) scoped.

var – var declarations are globally scoped when they are declared outside a function. However, when declared inside a function, var is function scoped.

const – a declaration for constants, const is also block scoped like let.

# Modules

Modules are similar to JavaScript libraries. There are many built-in modules like HTTP, URL, FS, Math and more that provide methods to handle their respective functions. To include a module, use the technique require(‘modulename’).

Example – require(‘HTTP’);

# NPM

A node package manager or NPM is a set of libraries that makes the work of a developer easy by providing –

  • In-built modules in the API.
  • Quickly loads third-party APIs, example express.
  • Create your own modules and call them using require(‘module’) function.

# Clustering

Clustering allows for multiple node.js processes to handle more load. With the cluster module, one can easily create child processes using spawn() and fork() methods. So, if you are asked a question as ‘how can you make node js multi-threaded,’ the answer is – clustering!

# Express framework

Express is a node js framework that makes coding easy and provides a robust set of core features like –

  • Dynamic rendering of HTML pages
  • Routing table that performs several actions based on URL and HTTP methods.
  • Setting up middleware to respond to HTTP requests.

1. Route handling

Through the Router class, express handles routing. This includes the routing functionality plus handling exceptions and errors like 404. Route handler also takes care of middleware routing.

2. Middleware

These are the functions that have access to req, res and the next middleware function to continue the stack. You can handle the stack manually with the middleware using the app.use function.

The interviewer can also ask you about the Response object and its properties which we have covered in our page on node js interview questions.

# File handling

File handling is done by including the ‘fs’ module in node js. This allows all the file operations like read, create, update, delete, write and rename files. You may be asked about simple asynchronous functions like readFile(), writeFile( and how data is passed on to a callback function. There are also corresponding synchronous methods that you should know about. You can try out these methods by writing simple code for practice.

# Event handling

Event handling is done using an event handler which is a callback function that is invoked once an event is triggered. Familiarize with the methods of EventEmitter module like emit(), addListener() and others to handle questions on event handlers.

# Database knowledge

It is essential to know about data connection to any of the supported DBs like MongoDB, Oracle, SQL Server, etc.… Don’t forget to read about necessary connection, simple insert and update to impress the interviewer.

# Unit Testing

You should know the basics of Jasmine or Mocha (unit testing frameworks) and how to write simple test cases for node js testing using either or both of them.

For a complete set of interview question and answers, check out node js interview questions. In general, ensure that you have thorough knowledge about all the above topics and can give simple examples for them. These are the most important topics that you should know in node js. All the best!

We have a collections of Node js interview Questions and answers pages.

Node js Interview Questions & Answers Advanced Node js Interview Questions and Answers