NodeJS is one of the important framework in MEAN stack. NodeJS is used to program server side part of applications. In this article, we present a set of NodeJS interview questions that could be helpful during interview process. The interview questions are divided into three categories based on the difficulty.

  • Entry Level – Node.js Interview Questions
  • Intermediate Level – Node.js Interview Questions
  • Adavanced Level – Node.js Interview Questions

NodeJS Interview Questions – Entry Level

Q1 - What is Node.js ?

Node.js is :

  • Open Source (Source code of Node.js is available openly and can be modified and built)
  • Cross Platform (Works on different operating systems)
  • Asynchronous
  • Event Driven (Web Requests are considered as events)
  • JavaScript Runtime Environment (JavaScript is run outside the browser)

Q2 - Which is the scripting/programming language used for Node.js application programming ?

The answer is one word. JavaScript.

Q3 - How is Node.js different from previous server side programming frameworks ?

Previous frameworks were not event driven. Node.js is a single thread driven.

Q4 - What are the features of Node.js ?

Some of the important features of Node.js are :

  • Asynchronous and Event Driven
  • Code Execution is very fast since Node.js is built on Chrome’s V8 JavaScript Engine.
  • Highly Scalable because of Event Looping.

Q5 - How is event-driven programming ?

Requests to the Node.js are considered as events. When a request is made, Node.js server accepts the request,  attaches it to a callback,  and starts preparing the response. When the response is being prepared, the server does not wait; it takes in other requests from clients. Whenever a response is ready, it is served via callback function. Thus everything is considered an event,

Q6 - What is MEAN stack ? What is the place of Node.js in the MEAN stack ?

MEAN is a JavaScript software stack. It is used for building dynamic websites and web applications. MEAN Stack components are :

  1. MongoDB
  2. Express.js
  3. Angular.js or Angular
  4. Node.js

Q7 - How do you install a module in node ?

A module is installed in node using NPM command line interface, npm. The command to install a module is : npm install <package>

Q8 - What is npm ?

npm stands for Node Package Manager. npm is the package manager for JavaScript.

Q9 - How is local installation of a dependency done ?

The default behavior of npm is to install the package/module local to the project. To install a dependency locally, navigate to the project folder and run the following command. npm install <package>

Q10 - How is global installation of a dependency done ?

To install a dependency globally, i.e., the module can be used in any node project on the machine, use global option in the npm install command as shown below : npm install <package> --global or npm install <package> --g

Q11 - How do you uninstall a dependency using npm ?

To uninstall a package or dependency, run npm unistall command with the package name to be removed. npm uninstall <package>

Q12 - What is the command for importing a package.

require command is used to include a package/module in a script file.

Example to include fs module in the script file is :

Q13 - How to add new functionalities to a module ?

New functions could be added to the existing modules. The step by step process is :

  1. Include the module in the script file using require statement.
  2. Add a function to the module using dot (.) operator.
  3. Export the module for the changes to take effect.

Following is an example where a new function named printMessage is added to fs module.

Reference : Node.js – Add new functions to module

Q14 - How to override functions of module ?

As required for the project, existing functions could be overridden.

The step by step process to override functions of a module is :

  1. Include the module in the script file.
  2. Delete the function (whose functionality has to be altered) using module variable.
  3. Add function with the same to the module variable.
  4. Export the module for the changes to take effect.

Following is an example, to demonstrate the same :

Reference : Node.js – Override functions of a module

Q15 - What is a callback ?

Callback function is used in Node.js to deal with event handling.

When an asynchronous function is called upon a resource for some task, the control is let immediately to continue with the subsequent statements after the function. The task on the resource would start in parallel. This helps Node.js continue with other tasks while the function is working with the resource. Once the task with the resource is completed, Node.js resumes with the callback function. Callback function is called with arguments : data object, result object and (or) error object containing information regarding the task.

An example for Node.js Callback function is :

Reference : Node.js – Callback Function

Q16 - What is callback hell ?

Callback hell is a situation when callback functions are nested. Nesting a callback function in another callback function leads to unmanageable code.

Q17 - How can you avoid callback hell ?

There are many procedures to avoid callback hell. Some of them are ;

  • Async
    • async.waterfall()
    • async.series()
  • Promises

Q18 - What is a Promise in Node.js ?

Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers to an asynchronous action’s eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of the final value, the asynchronous method returns a promise for the value at some point in the future.

Q19 - What module do you use for accessing/modifying files in Node.js ?

Node FS (File System) can be used for file operations. Node FS is a builtin module. It can be included in the script using the following statement :

Some of the functions fs module provides are :

  • fs.readFile()
  • fs.writeFile()
  • fs.appendFile()
  • fs.open()
  • fs.rename()
  • fs.unlink()

The above mentioned functions are asynchronous. Synchronous variants for these are also available.

Q20 - What is chaining in Node.js ?

Each function of a module returns the modified variable. So, another function call can be concatenated to the previous function call.

Following is a non-working example for chaining in Node.js :

Q21 - How to exit from a Node.js Script ?

The execution of a Node.js script happens sequentially. For some reason, if you want to exit, the built-in process module has exit() method.

Q22 - How to ensure that the dependencies in a Node.js project are secure ?

Q23 - How do you redirect a url to other in Node.js ?

http module can be used to setup an url redirect.

In the following example, we created an HTTP server. When the server gets a request for a resource, we can send a redirect response back to the browser using response.writeHead() function. The response code should be 301 for a redirect and the redirect url is provided as well in writeHead() function.

Q24 - What is Buffer in Node.js ?

Node.js Buffer is a class that helps to handle and work with octet streams. Octet streams would generally come into picture when dealing with TCP data streams and file system operations.

Q25 - Give us an example to create HTTP web server ?

We can use http module to create a HTTP web server in Node.js.

Brief steps to create HTTP web server :

  1.  Include http module.
  2. Create http server to listen on a port.
  3. Write program statements to prepare response.
  4. Run the script to start the web server.
  5. From the same machine, you can request the server using localhost:port

An example script to create a web server is :

Reference : Node.js – Create HTTP Web Server

Q26 - How do you connect your Node.js application to MySQL ?

From Node.js, we can connect to MySQL server. We can use mysql module.

  1. Include mysql module in the script file.
  2. Create a connection variable with the information : MySQL Server IP, Port, Username, Password.
  3. Call connect method using connection variable. If the setup is fine, the callback function should receive a null error object.

An example Node.js script to connect to MySQL server :

Reference : Node.js – Connect to MySQL Database

Q27 - What is a result object in Node.js MySQL ?

When a MySQL Query is executed in Node.js, an object called Result Object is returned to the callback function. The Result Object contains result set or properties that provide information regarding the execution of a query in MySQL Server.

The data in result object depends on the type of query that is made to MySQL Server.

Q28 - How do you connect to MongoDB from Node.js application ?

Q29 - What is try-catch in Node.js ?

Q30 - Write a program to upload files to Node.js server ?