node.js interview questions

Table of Contents

Sl.No Questions
01. What does the runtime environment mean in Node.js?
02. What is Node.js?
03. What is Node.js Process Model?
04. What are the data types in Node.js?
05. How to create a simple server in Node.js that returns Hello World?
06. How do Node.js works?
07. What is an error-first callback?
08. What is callback hell in Node.js?
09. What are Promises in Node.js?
10. What tools can be used to assure consistent style?
11. When should you npm and when yarn?
12. What is a stub?
13. What is a test pyramid? How can you implement it when talking about HTTP APIs?
14. How can you secure your HTTP cookies against XSS attacks?
15. How can you make sure your dependencies are safe?
16. What is Event loop in Node.js? And How does it work?
17. What is REPL? What purpose it is used for?
18. What is the difference between Asynchronous and Non-blocking?
19. How to debug an application in Node.js?
20. What are some of the most popular modules of Node.js?
21. What is EventEmitter in Node.js?
22. How many types of streams are present in node.js?
23. What is crypto in Node.js? How do you cipher the secure information in Node.js?
24. What is the use of DNS module in Node.js?
25. What are the security mechanisms available in Node.js?
26. Name the types of API functions in Node.js.
27. How does Node.js handle child threads?
28. What is the preferred method of resolving unhandled exceptions in Node.js?
29. How does Node.js support multi-processor platforms, and does it fully utilize all processor resources?
30. What is typically the first argument passed to a Node.js callback handler?
31. How Node.js read the content of a file?
32. What is JIT and how is it related to Node.js?
33. What is difference between put and patch?
34. List types of Http requests supported by Node.js.
35. Why to use Express.js?
36. Write the steps for setting up an Express JS application.
37. Since node is a single threaded process, how to make use of all CPUs?
38. What does emitter do and what is dispatcher?
39. How to kill child processes that spawn their own child processes in Node.js?
40. What do you understand by Reactor Pattern in Node.js?
41. What are the key features of Node.js?
42. What are globals in Node.js?
43. What is chaining process in Node.js?
44. What is a control flow function? what are the steps does it execute?
45. What is npm in Node.js?
46. When to use Node.js and when not to use it?
47. Explain how does Node.js work?
48. Is Node.js entirely based on a single-thread?
49. How to make post request in Node.js?
50. Can you create http server in Node.js, explain the code used for it?
51. How to load html in Node.js?
52. How can you listen on port 80 with Node?
53. What is the difference between operational and programmer errors?
54. Why npm shrinkwrap is useful?
55. What is your favourite HTTP framework and why?
56. What are the Challenges with Node.js?
57. What is the difference between Node.js vs Ajax?
58. How Node.js overcomes the problem of blocking of I/O operations?
59. Mention the steps by which you can async in Node.js?
60. What are the timing features of Node.js?
61. What is LTS releases of Node.js why should you care?
62. Why should you separate Express ‘app’ and ‘server’?
63. What is the difference between process.nextTick() and setImmediate()?
64. What is difference between JavaScript and Node.js?
65. What are the difference between Events and Callbacks?
66. Explain RESTful Web Services in Node.js?
67. How to handle file upload in Node js?
68. Explain the terms body-parser, cookie-parser, debug, jade, morgan, nodemon, pm2, serve-favicon, cors in Express JS?
69. How does routing work in Node.js
70. How Node prevents blocking code?
71. What is difference between promise and async await in node js?
72. How to use JSON Web Token (JWT) for authentication in node js?
73. How to build a microservices architecture with node js?
74. How to use Q promise in node js?
75. How to use locale (i18n) in node js?
76. How to Implement Memcached in Nodejs?
77. Explain Error Handling in Nodejs?
78. How to generate and verify checksum of the given string in Nodejs

Q. What does the runtime environment mean in Node.js?

The Node.js runtime is the software stack responsible for installing your web service's code and its dependencies and running your service.

The Node.js runtime for App Engine in the standard environment is declared in the app.yaml file:

1
runtime: nodejs10

The runtime environment is literally just the environment your application is running in. This can be used to describe both the hardware and the software that is running your application. How much RAM, what version of node, what operating system, how much CPU cores, can all be referenced when talking about a runtime environment.

Q. What is Node.js?

Node.js is an open-source server side runtime environment built on Chrome's V8 JavaScript engine. It provides an event driven, non-blocking (asynchronous) I/O and cross-platform runtime environment for building highly scalable server-side applications using JavaScript.

Q. What is Node.js Process Model?

Node.js runs in a single process and the application code runs in a single thread and thereby needs less resources than other platforms. All the user requests to your web application will be handled by a single thread and all the I/O work or long running job is performed asynchronously for a particular request. So, this single thread doesn’t have to wait for the request to complete and is free to handle the next request. When asynchronous I/O work completes then it processes the request further and sends the response.

Q. What are the data types in Node.js?

Primitive Types

  • String

  • Number

  • Boolean

  • Undefined

  • Null

  • RegExp

  • Buffer: Node.js includes an additional data type called Buffer (not available in browser's JavaScript). Buffer is mainly used to store binary data, while reading from a file or receiving packets over the network.

Q. How to create a simple server in Node.js that returns Hello World?

Step 01: Create a project directory

1
2
mkdir myapp
cd myapp

Step 02: Initialize project and link it to npm

1
npm init

This creates a package.json file in your myapp folder. The file contains references for all npm packages you have downloaded to your project. The command will prompt you to enter a number of things.
You can enter your way through all of them EXCEPT this one:

1
entry point: (index.js)

Rename this to:

1
app.js

Step 03: Install Express in the myapp directory

1
npm install express --save

Step 04: app.js

1
2
3
4
5
6
7
8
9
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});

app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});

Step 05: Run the app

1
node app.js

Q. How do Node.js works?

Node is completely event-driven. Basically the server consists of one thread processing one event after another.

A new request coming in is one kind of event. The server starts processing it and when there is a blocking IO operation, it does not wait until it completes and instead registers a callback function. The server then immediately starts to process another event (maybe another request). When the IO operation is finished, that is another kind of event, and the server will process it (i.e. continue working on the request) by executing the callback as soon as it has time.

So the server never needs to create additional threads or switch between threads, which means it has very little overhead. If you want to make full use of multiple hardware cores, you just start multiple instances of node.js

Node JS Platform does not follow Request/Response Multi-Threaded Stateless Model. It follows Single Threaded with Event Loop Model. Node JS Processing model mainly based on Javascript Event based model with Javascript callback mechanism.

Single Threaded Event Loop Model Processing Steps:

  • Clients Send request to Web Server.
  • Node JS Web Server internally maintains a Limited Thread pool to provide services to the Client Requests.
  • Node JS Web Server receives those requests and places them into a Queue. It is known as “Event Queue”.
  • Node JS Web Server internally has a Component, known as “Event Loop”. Why it got this name is that it uses indefinite loop to receive requests and process them.
  • Event Loop uses Single Thread only. It is main heart of Node JS Platform Processing Model.
  • Even Loop checks any Client Request is placed in Event Queue. If no, then wait for incoming requests for indefinitely.
  • If yes, then pick up one Client Request from Event Queue
    • Starts process that Client Request
    • If that Client Request Does Not requires any Blocking IO Operations, then process everything, prepare response and send it back to client.
    • If that Client Request requires some Blocking IO Operations like interacting with Database, File System, External Services then it will follow different approach
      • Checks Threads availability from Internal Thread Pool
      • Picks up one Thread and assign this Client Request to that thread.
      • That Thread is responsible for taking that request, process it, perform Blocking IO operations, prepare response and send it back to the Event Loop
      • Event Loop in turn, sends that Response to the respective Client.

Q. What is an error-first callback?

The pattern used across all the asynchronous methods in Node.js is called Error-first Callback. Here is an example:

1
2
3
4
5
6
fs.readFile( "file.json", function ( err, data ) {
if ( err ) {
console.error( err );
}
console.log( data );
});

Any asynchronous method expects one of the arguments to be a callback. The full callback argument list depends on the caller method, but the first argument is always an error object or null. When we go for the asynchronous method, an exception thrown during function execution cannot be detected in a try/catch statement. The event happens after the JavaScript engine leaves the try block.

In the preceding example, if any exception is thrown during the reading of the file, it lands on the callback function as the first and mandatory parameter.

Q. What is callback hell in Node.js?

Callback hell is a phenomenon that afflicts a JavaScript developer when he tries to execute multiple asynchronous operations one after the other.

An asynchronous function is one where some external activity must complete before a result can be processed; it is “asynchronous” in the sense that there is an unpredictable amount of time before a result becomes available. Such functions require a callback function to handle errors and process the result.

1
2
3
4
5
6
7
8
9
10
11
getData(function(a){
getMoreData(a, function(b){
getMoreData(b, function(c){
getMoreData(c, function(d){
getMoreData(d, function(e){
...
});
});
});
});
});

Techniques for avoiding callback hell

  1. Using Async.js
  2. Using Promises
  3. Using Async-Await
  • Managing callbacks using Async.js

Async is a really powerful npm module for managing asynchronous nature of JavaScript. Along with Node.js, it also works for JavaScript written for browsers.

Async provides lots of powerful utilities to work with asynchronous processes under different scenarios.

1
npm install --save async
  • ASYNC WATERFALL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var async = require('async');
async.waterfall([
function(callback) {
//doSomething
callback(null, paramx); //paramx will be availaible as the first parameter to the next function
/**
The 1st parameter passed in callback.
@null or @undefined or @false control moves to the next function
in the array
if @true or @string the control is immedeatly moved
to the final callback fucntion
rest of the functions in the array
would not be executed
*/
},
function(arg1, callback) {
//doSomething else
// arg1 now equals paramx
callback(null, result);
},
function(arg1, callback) {
//do More
// arg1 now equals 'result'
callback(null, 'done');
},
function(arg1, callback) {
//even more
// arg1 now equals 'done'
callback(null, 'done');
}
], function (err, result) {
//final callback function
//finally do something when all function are done.
// result now equals 'done'
});
  • ASYNC SERIES
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var async = require('async');
async.series([
function(callback){
// do some stuff ...
callback(null, 'one');
/**
The 1st parameter passed in callback.
@null or @undefined or @false control moves to the next function
in the array
if @true or @string the control is immedeatly moved
to the final callback fucntion with the value of err same as
passed over here and
rest of the functions in the array
would not be executed
*/
},
function(callback){
// do some more stuff ...
callback(null, 'two');
}
],
// optional callback
function(err, results){
// results is now equal to ['one', 'two']
});
  • Managing callbacks hell using promises

Promises are alternative to callbacks while dealing with asynchronous code. Promises return the value of the result or an error exception. The core of the promises is the .then() function, which waits for the promise object to be returned. The .then() function takes two optional functions as arguments and depending on the state of the promise only one will ever be called. The first function is called when the promise if fulfilled (A successful result). The second function is called when the promise is rejected.

1
2
3
4
5
var outputPromise = getInputPromise().then(function (input) {
//handle success
}, function (error) {
//handle error
});
  • Using Async Await

Async await makes asynchronous code look like it\’s synchronous. This has only been possible because of the reintroduction of promises into node.js. Async-Await only works with functions that return a promise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const getrandomnumber = function(){
return new Promise((resolve, reject)=>{
setTimeout(() => {
resolve(Math.floor(Math.random() * 20));
}, 1000);
});
}

const addRandomNumber = async function(){
const sum = await getrandomnumber() + await getrandomnumber();
console.log(sum);
}

addRandomNumber();

Q. What are Promises in Node.js?

It allows 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.

Promises in node.js promised to do some work and then had separate callbacks that would be executed for success and failure as well as handling timeouts. Another way to think of promises in node.js was that they were emitters that could emit only two events: success and error.The cool thing about promises is you can combine them into dependency chains (do Promise C only when Promise A and Promise B complete).

The core idea behind promises is that a promise represents the result of an asynchronous operation. A promise is in one of three different states:

  • pending - The initial state of a promise.
  • fulfilled - The state of a promise representing a successful operation.
  • rejected - The state of a promise representing a failed operation.
    Once a promise is fulfilled or rejected, it is immutable (i.e. it can never change again).

Creating a Promise

1
2
3
var myPromise = new Promise(function(resolve, reject){
....
})

Q. What tools can be used to assure consistent style?

  • ESLint
  • Standard

Q. When should you npm and when yarn?

  • npm

It is the default method for managing packages in the Node.js runtime environment. It relies upon a command line client and a database made up of public and premium packages known as the the npm registry. Users can access the registry via the client and browse the many packages available through the npm website. Both npm and its registry are managed by npm, Inc.

1
2
node -v
npm -v
  • Yarn

Yarn was developed by Facebook in attempt to resolve some of npm’s shortcomings. Yarn isn’t technically a replacement for npm since it relies on modules from the npm registry. Think of Yarn as a new installer that still relies upon the same npm structure. The registry itself hasn’t changed, but the installation method is different. Since Yarn gives you access to the same packages as npm, moving from npm to Yarn doesn’t require you to make any changes to your workflow.

1
npm install yarn --global

Comparing Yarn vs npm

  • Fast: Yarn caches every package it downloads so it never needs to again. It also parallelizes operations to maximize resource utilization so install times are faster than ever.
  • Reliable: Using a detailed, but concise, lockfile format, and a deterministic algorithm for installs, Yarn is able to guarantee that an install that worked on one system will work exactly the same way on any other system.
  • Secure: Yarn uses checksums to verify the integrity of every installed package before its code is executed.
  • Offline Mode: If you’ve installed a package before, you can install it again without any internet connection.
  • Deterministic: The same dependencies will be installed the same exact way across every machine regardless of install order.
  • Network Performance: Yarn efficiently queues up requests and avoids request waterfalls in order to maximize network utilization.
  • Multiple Registries: Install any package from either npm or Bower and keep your package workflow the same.
  • Network Resilience: A single request failing won’t cause an install to fail. Requests are retried upon failure.
  • Flat Mode: Resolve mismatching versions of dependencies to a single version to avoid creating duplicates.

Q. What is a stub?

Stubbing and verification for node.js tests. Enables you to validate and override behaviour of nested pieces of code such as methods, require() and npm modules or even instances of classes. This library is inspired on node-gently, MockJS and mock-require.

Features of Stub:

  • Produces simple, lightweight Objects capable of extending down their tree
  • Compatible with Nodejs
  • Easily extendable directly or through an ExtensionManager
  • Comes with predefined, usable extensions

Stubs are functions/programs that simulate the behaviours of components/modules. Stubs provide canned answers to function calls made during test cases. Also, you can assert on with what these stubs were called.

A use-case can be a file read, when you do not want to read an actual file:

1
2
3
4
5
6
7
8
var fs = require('fs');

var readFileStub = sinon.stub(fs, 'readFile', function (path, cb) {
return cb(null, 'filecontent');
});

expect(readFileStub).to.be.called;
readFileStub.restore();

Q. What is a test pyramid? How can you implement it when talking about HTTP APIs?

The “Test Pyramid” is a metaphor that tells us to group software tests into buckets of different granularity. It also gives an idea of how many tests we should have in each of these groups. It shows which kinds of tests you should be looking for in the different levels of the pyramid and gives practical examples on how these can be implemented.

Test Pyramid

Mike Cohn's original test pyramid consists of three layers that your test suite should consist of (bottom to top):

  1. Unit Tests
  2. Service Tests
  3. User Interface Tests

Q. How can you secure your HTTP cookies against XSS attacks?

1. When the web server sets cookies, it can provide some additional attributes to make sure the cookies won't be accessible by using malicious JavaScript. One such attribute is HttpOnly.

1
Set-Cookie: [name]=[value]; HttpOnly

HttpOnly makes sure the cookies will be submitted only to the domain they originated from.

2. The “Secure” attribute can make sure the cookies are sent over secured channel only.

1
Set-Cookie: [name]=[value]; Secure

3. The web server can use X-XSS-Protection response header to make sure pages do not load when they detect reflected cross-site scripting (XSS) attacks.

1
X-XSS-Protection: 1; mode=block

4. The web server can use HTTP Content-Security-Policy response header to control what resources a user agent is allowed to load for a certain page. It can help to prevent various types of attacks like Cross Site Scripting (XSS) and data injection attacks.

1
Content-Security-Policy: default-src 'self' *.http://sometrustedwebsite.com

Q. How can you make sure your dependencies are safe?

The only option is to automate the update / security audit of your dependencies. For that there are free and paid options:

  1. npm outdated
  2. Trace by RisingStack
  3. NSP
  4. GreenKeeper
  5. Snyk
  6. npm audit
  7. npm audit fix

Q. What is Event loop in Node.js? And How does it work?

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.

Node.js is a single-threaded application, but it can support concurrency via the concept of event and callbacks. Every API of Node.js is asynchronous and being single-threaded, they use async function calls to maintain concurrency. Node uses observer pattern. Node thread keeps an event loop and whenever a task gets completed, it fires the corresponding event which signals the event-listener function to execute.

Event-Driven Programming

In an event-driven application, there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected.

Although events look quite similar to callbacks, the difference lies in the fact that callback functions are called when an asynchronous function returns its result, whereas event handling works on the observer pattern. The functions that listen to events act as Observers. Whenever an event gets fired, its listener function starts executing. Node.js has multiple in-built events available through events module and EventEmitter class which are used to bind events and event-listeners as follows

1
2
3
4
5
// Import events module
var events = require('events');

// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Import events module
var events = require('events');

// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

// Create an event handler as follows
var connectHandler = function connected() {
console.log('connection succesful.');

// Fire the data_received event
eventEmitter.emit('data_received');
}

// Bind the connection event with the handler
eventEmitter.on('connection', connectHandler);

// Bind the data_received event with the anonymous function
eventEmitter.on('data_received', function() {
console.log('data received succesfully.');
});

// Fire the connection event
eventEmitter.emit('connection');

console.log("Program Ended.");

Q. What is REPL? What purpose it is used for?

REPL (READ, EVAL, PRINT, LOOP) is a computer environment similar to Shell (Unix/Linux) and command prompt. Node comes with the REPL environment when it is installed. System interacts with the user through outputs of commands/expressions used. It is useful in writing and debugging the codes. The work of REPL can be understood from its full form:

  • Read: It reads the inputs from users and parses it into JavaScript data structure. It is then stored to memory.
  • Eval: The parsed JavaScript data structure is evaluated for the results.
  • Print: The result is printed after the evaluation.
  • Loop: Loops the input command. To come out of NODE REPL, press ctrl+c twice

Simple Expression

1
2
3
4
5
6
$ node
> 10 + 20
30
> 10 + ( 20 * 30 ) - 40
570
>

Q. What is the difference between Asynchronous and Non-blocking?

1. Asynchronous

The architecture of asynchronous explains that the message sent will not give the reply on immediate basis just like we send the mail but do not get the reply on an immediate basis. It does not have any dependency or order. Hence improving the system efficiency and performance. The server stores the information and when the action is done it will be notified.

2. Non-Blocking

Nonblocking immediately responses with whatever data available. Moreover, it does not block any execution and keeps on running as per the requests. If an answer could not be retrieved than in those cases API returns immediately with an error. Nonblocking is mostly used with I/O(input/output). Node.js is itself based on nonblocking I/O model. There are few ways of communication that a nonblocking I/O has completed. The callback function is to be called when the operation is completed. Nonblocking call uses the help of javascript which provides a callback function.

  • Asynchronous VS Non-Blocking

1) Asynchronous does not respond immediately, While Nonblocking responds immediately if the data is available and if not that simply returns an error.
2) Asynchronous improves the efficiency by doing the task fast as the response might come later, meanwhile, can do complete other tasks. Nonblocking does not block any execution and if the data is available it retrieves the information quickly.
3) Asynchronous is the opposite of synchronous while nonblocking I/O is the opposite of blocking. They both are fairly similar but they are also different as asynchronous is used with a broader range of operations while nonblocking is mostly used with I/O.

Q. How to debug an application in Node.js?

  • node-inspector
1
npm install -g node-inspector

Run

1
node-debug app.js
  • Debugging

    • Debugger
    • Node Inspector
    • Visual Studio Code
    • Cloud9
    • Brackets
  • Profiling

1
2
1. node --prof ./app.js
2. node --prof-process ./the-generated-log-file
  • Heapdumps

    • node-heapdump with Chrome Developer Tools
  • Tracing

    • Interactive Stack Traces with TraceGL
  • Logging
    Libraries that output debugging information

    • Caterpillar
    • Tracer
    • scribbles

Libraries that enhance stack trace information

  • Longjohn
  • Async: Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.
  • Browserify: Browserify will recursively analyze all the require() calls in your app in order to build a bundle you can serve up to the browser in a single

请我喝杯咖啡吧~

支付宝
微信