7 Hardest Node.js Interview Questions & Answers
Tags: node, interview, questions, fullstack
Published: 2018-07-18 14:15:01

---
title: 7 Hardest Node.js Interview Questions & Answers
published: true
description: 7 Hardest Node.js Interview Questions & Answers
tags: nodejs, interview, questions, fullstack
canonical_url: https://www.fullstack.cafe/blog/7-hardest-nodejs-interview-questions-and-answers
---
![7 Hardest Node.js Interview Questions & Answers](https://images.pexels.com/photos/674268/pexels-photo-674268.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=350)
Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code server-side. Node.js lets developers use JavaScript for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.

> 🔴 Originally published on [FullStack.Cafe - Kill Your Tech & Coding Interview](https://www.fullstack.cafe/?utm_source=dev&utm_medium=blog)

### Q1: Provide some example of config file separation for dev and prod environments
> Topic: **Node.js**<br/>Difficulty: ⭐⭐⭐⭐

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:

```js
var config = {
  production: {
    mongo : {
      billing: '****'
    }
  },
  default: {
    mongo : {
      billing: '****'
    }
  }
}

exports.get = function get(env) {
  return config[env] || config.default;
}
```
And it's usage:
```js
const config = require('./config/config.js').get(process.env.NODE_ENV);
const dbconn = mongoose.createConnection(config.mongo.billing);
```

🔗**Source:** [github.com/i0natan/nodebestpractices](https://github.com/i0natan/nodebestpractices/blob/master/sections/projectstructre/configguide.md)
### Q2: What are the timing features of Node.js?
> Topic: **Node.js**<br/>Difficulty: ⭐⭐⭐⭐

The Timers module in Node.js contains functions that execute code after a set period of time. 

* **setTimeout/clearTimeout** - can be used to schedule code execution after a designated amount of milliseconds
* **setInterval/clearInterval** - can be used to execute a block of code multiple times 
* **setImmediate/clearImmediate** - will execute code at the end of the current event loop cycle
* **process.nextTick** - used to schedule a callback function to be invoked in the next iteration of the Event Loop

```js
function cb(){
  console.log('Processed in next iteration');
}
process.nextTick(cb);
console.log('Processed in the first iteration');
```

Output:

```js
Processed in the first iteration
Processed in next iteration
```

🔗**Source:** [github.com/jimuyouyou](https://github.com/jimuyouyou/node-interview-questions)
### Q3: Explain what is Reactor Pattern in Node.js?
> Topic: **Node.js**<br/>Difficulty: ⭐⭐⭐⭐⭐

**Reactor Pattern** is an idea of non-blocking I/O operations in Node.js. This pattern provides a handler(in case of Node.js, a *callback function*) that is associated with each I/O operation. When an I/O request is generated, it is submitted to a *demultiplexer*.

This *demultiplexer* is a notification interface that is used to handle concurrency in non-blocking I/O mode and collects every request in form of an event and queues each event in a queue. Thus, the demultiplexer provides the *Event Queue*. 

At the same time, there is an Event Loop which iterates over the items in the Event Queue. Every event has a callback function associated with it, and that callback function is invoked when the Event Loop iterates.

🔗**Source:** [hackernoon.com](https://hackernoon.com/the-node-js-system-51090c35dddc)
### Q4: What is LTS releases of Node.js why should you care?
> Topic: **Node.js**<br/>Difficulty: ⭐⭐⭐⭐⭐

An **LTS(Long Term Support)** version of Node.js receives all the critical bug fixes, security updates and performance improvements. 

LTS versions of Node.js are supported for at least 18 months and are indicated by even version numbers (e.g. 4, 6, 8). They're best for production since the LTS release line is focussed on stability and security, whereas the *Current* release line has a shorter lifespan and more frequent updates to the code. Changes to LTS versions are limited to bug fixes for stability, security updates, possible npm updates, documentation updates and certain performance improvements that can be demonstrated to not break existing applications.

🔗**Source:** [github.com/i0natan/nodebestpractices](https://github.com/i0natan/nodebestpractices/blob/master/sections/production/LTSrelease.md)
### Q5: Why should you separate Express 'app' and 'server'?
> Topic: **Node.js**<br/>Difficulty: ⭐⭐⭐⭐⭐

Keeping the API declaration separated from the network related configuration (port, protocol, etc) allows testing the API in-process, without performing network calls, with all the benefits that it brings to the table: fast testing execution and getting coverage metrics of the code. It also allows deploying the same API under flexible and different network conditions. Bonus: better separation of concerns and cleaner code.

API declaration, should reside in app.js:

```js
var app = express();
app.use(bodyParser.json());
app.use("/api/events", events.API);
app.use("/api/forms", forms);
```
Server network declaration, should reside in /bin/www:

```js
var app = require('../app');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);
```

🔗**Source:** [github.com/i0natan/nodebestpractices](https://github.com/i0natan/nodebestpractices/blob/master/sections/projectstructre/separateexpress.md)
### Q6: What is the difference between process.nextTick() and setImmediate() ?
> Topic: **Node.js**<br/>Difficulty: ⭐⭐⭐⭐⭐

The difference between `process.nextTick()` and `setImmediate()` is that `process.nextTick()` defers the execution of an action till the next pass around the event loop or it simply calls the callback function once the ongoing execution of the event loop is finished whereas `setImmediate()` executes a callback on the next cycle of the event loop and it gives back to the event loop for executing any I/O operations.

🔗**Source:** [codingdefined.com](https://www.codingdefined.com/2017/04/top-20-interview-questions-on-nodejs.html)
### Q7: Rewrite the code sample without try/catch block
> Topic: **Node.js**<br/>Difficulty: ⭐⭐⭐⭐⭐

Consider the code:
```js
async function check(req, res) {
  try {
    const a = await someOtherFunction();
    const b = await somethingElseFunction();
    res.send("result")
  } catch (error) {
    res.send(error.stack);
  }
}
```
Rewrite the code sample without try/catch block.

**Answer:**
```js
async function getData(){
  const a = await someFunction().catch((error)=>console.log(error));
  const b = await someOtherFunction().catch((error)=>console.log(error));
  if (a && b) console.log("some result")
}
```
or if you wish to know which specific function caused error:
```js
async function loginController() {
  try {
    const a = await loginService().
    catch((error) => {
      throw new CustomErrorHandler({
        code: 101,
        message: "a failed",
        error: error
      })
    });
    const b = await someUtil().
    catch((error) => {
      throw new CustomErrorHandler({
        code: 102,
        message: "b failed",
        error: error
      })
    });
    //someoeeoe
    if (a && b) console.log("no one failed")
  } catch (error) {
    if (!(error instanceof CustomErrorHandler)) {
      console.log("gen error", error)
    }
  }
}
```

🔗**Source:** [medium.com](https://medium.com/tech-buddy/async-await-without-try-catch-in-javascript-fdd38abf7e90)

> *Thanks 🙌 for reading and good luck on your interview!* <br/>*Check more FullStack Interview Questions & Answers on 👉 [www.fullstack.cafe](https://www.fullstack.cafe)*