Categories
Top 50 Node JS Interview Questions and Answers
Node.js is a super popular server-side platform that more and more organizations are using. If you are preparing for careers and have an upcoming job interview, it’s always a good idea to prepare and brush up on your interview skills beforehand. Although there are a few commonly asked Node.js interview questions that pop up during all types of interviews, we also recommend that you prepare by focusing on exclusive questions to your specific industry.
1. What is Node.js? Where can you use it?
Node.js is perfect for data-intensive applications as it uses an asynchronous, event-driven model. You can use I/O intensive web applications like video streaming sites. You can also use it for developing: Real-time web applications, Network applications, General-purpose
2. Why is Node.js Single-threaded?
3. Why use Node.js?
4. Node.js makes building scalable network programs easy. Some of its advantages include:
b) It rarely blocks
c) It offers a unified programming language and data type
d) Everything is asynchronous
e) It yields great concurrency
5. How does Node.js work?
2) Querying for data
3) Deleting data
4) Updating the data
5) Node.js retrieves the incoming requests and adds those to the Event Queue
6) The requests are then passed one-by-one through the Event Loop. It checks if the requests are simple enough not to require any external resources
7) The Event Loop processes simple requests (non-blocking operations), such as I/O Polling, and returns the responses to the corresponding clients
A single thread from the Thread Pool is assigned to a single complex request. This thread is responsible for completing a particular blocking request by accessing external resources, such as computation, database, file system, etc.
6. What are the advantages of using promises instead of callbacks?
2) The coupling is low.
3) We've built-in error handling.
4) Improved readability.
7. If Node.js is single-threaded, then how does it handle concurrency?
8. Explain callback in Node.js.
9. How is Node.js most frequently used?
1) Real-time chats
2) Internet of Things
3) Complex SPAs (Single-Page Applications)
4) Real-time collaboration tools
5) Streaming applications
6) Microservices architecture
10. How would you define the term I/O?
Every transfer is an output from one medium and an input into another. The medium can be a physical device, network, or files within a system
11. Explain the difference between frontend and backend development?
Front-end | Back-end |
Frontend refers to the client-side of an application | Backend refers to the server-side of an application |
It is the part of a web application that users can see and interact with | It constitutes everything that happens behind the scenes |
It typically includes everything that attributes to the visual aspects of a web application | It generally includes a web server that communicates with a database to serve requests |
HTML, CSS, JavaScript, AngularJS, and ReactJS are some of the essentials of frontend development | Java, PHP, Python, and Node.js are some of the backend development technologies |
12. What are the pros and cons of Node.js?
Node.js Pros | Node.js Cons |
Fast processing and an event-based model | Not suitable for heavy computational tasks |
Uses JavaScript, which is well-known amongst developers | Using callback is complex since you end up with several nested callbacks |
Node Package Manager has over 50,000 packages that provide the functionality to an application | Dealing with relational databases is not a good option for Node.js |
Best suited for streaming huge amounts of data and I/O intensive operations | Since Node.js is single-threaded, CPU intensive tasks are not its strong suit |
13. What is the difference between Angular and Node.js?
Angular | Node.js |
It is a frontend development framework | It is a server-side environment |
It is written in TypeScript | It is written in C, C++ languages |
Used for building single-page, client-side web applications | Used for building fast and scalable server-side networking applications |
Splits a web application into MVC components | Generates database queries |
14. What is NPM?
Node Package Manager provides two main functionalities:
1) Provides online repositories for node.js packages/modules, which are searchable on search.nodejs.org
2) Provides online repositories for node.js packages/modules, which are searchable on search.nodejs.org
15. Why is Node.js preferred over other backend technologies like Java and PHP?
1) Node.js is very fast
2) Node Package Manager has over 50,000 bundles available at the developer’s disposal
3) Perfect for data-intensive, real-time web applications, as Node.js never waits for an API to return data
4) Better synchronization of code between server and client due to same code base
5) Easy for web developers to start using Node.js in their projects as it is a JavaScript library
16. Which database is more popularly used with Node.js?
17. What is the purpose of the module .Exports?
18. Why is Node.js preferred over other backend technologies like Java and PHP?
1) Node.js is very fast
2) Node Package Manager has over 50,000 bundles available at the developer’s disposal
3) Perfect for data-intensive, real-time web applications, as Node.js never waits for an API to return data
4) Better synchronization of code between server and client due to same code base
5) Easy for web developers to start using Node.js in their projects as it is a JavaScript library
19. What is the command used to import external libraries?
Now that we have covered some of the important beginner-level Node.js interview questions let us look at some of the intermediate-level Node.js interview questions.
20. What are the modules in Node.js?
Node.js has many modules to provide the basic functionality needed for a web application. Some of them include:
Core Modules | Description |
HTTP | Includes classes, methods, and events to create a Node.js HTTP server |
util | Includes utility functions useful for developers |
fs | Includes events, classes, and methods to deal with file I/O operations |
url | Includes methods to work with query string |
query string | Includes methods to work with query string |
stream | Includes methods to handle streaming data |
zlib | Includes methods to compress or decompress files |
Node.js Interview Questions and Answers For Intermediate Level
21. What does event-driven programming mean?
22. What are the two types of API functions in Node.js?
1) Asynchronous, non-blocking functions
2) Synchronous, blocking functions
23.What is an EventEmitter in Node.js?
Whenever an object from the EventEmitter class throws an event, all attached functions are called upon synchronously
24. Differentiate between process.nextTick() and setImmediate()?
25. What is an Event Loop in Node.js?
26. What is the Express.js package?
27. What are streams in Node.js?
Readable
Used for reading operations<Writable
Used for write operationsDuplex
Can be used for both reading and write operationsTransform
A type of duplex stream where the output is computed based on input28. What is the package.json file?
This is what a package.json file looks like immediately after creating a Node.js project using the command: npm init
You can edit the parameters when you create a Node.js project.
29. How would you use a URL module in Node.js?
var url = require('url');
var adrs = "http://localhost:8080/default.hrm?year=2020&month=match';"
var que = url.parse(adrs, true);
console.log(que.host);//returns 'localhost:8080'
console.log(que,pathname); //returns '/default.htm'
console.log(que.search); //returns '?year=2020 and month=march'
var quedata = que.query; //returns an object: { year: 2020,month:'march'}
console.log(quedata.month); //returns 'march'
30. How do you create a simple Express.js application?
The response object represents the HTTP response that an Express app sends when it receives an HTTP request
var express = require( 'express' );
var app = express();
app.get('/' ,function(req,res){
res.send('hello word');
})
var server = app.listen(8081,function(){
var host = server.address().address
var port = server.address().port
console.log("example app listing at http://%s:%s" ,host,port)
})
31. How do we implement async in Node.js?
async function fun1(req,res){
let response = await request.get('http://localhost:3000');
if(response.err){ console.log('error');}
else { console.log('fetched response');
}
32. How do you create a simple server in Node.js that returns Hello World?
2) Use createServer function with a callback function using request and response as parameters.
3) Type “hello world."
4) Set the server to listen to port 8080 and assign an IP address
var http = require('http);
http.createserver(function(req,res){
res.writehead(200,{'content-type':'text/plain'});
res.end('hello world\n');
}).listen(8080,'727.0.01');
33.Explain asynchronous and non-blocking APIs in Node.js.
2) A Node.js-based server never waits for an API to return data. Instead, it moves to the next API after calling it, and a notification mechanism from a Node.js event responds to the server for the previous API call
34. What is a callback function in Node.js?
Node.js Interview Questions and Answers For Experienced Professionals
This section will provide you with the Advanced Node.js interview questions which will primarily help experienced professionals.
35. What is the control flow function?
36. What is REPL in Node.js?
REPL Perform the following desired tasks:
Read: read users input, parses the input into JavaScript data structure and store memory
Eval: Takes and evaluate the data structure
Print: Print the result
Loop: Loops the above command untill user presses ctrl-c twice
37. How does control flow manage the function calls?
1) Control the order of execution
2) Collect Data
3) Limit Concurrency
4) Call the next step program
38. What is piping in Node.js?
39. How to Change my Photo from Admin Dashboard?
40. What is the difference between fork() and spawn() methods in Node.js?
fork() | spawn() |
fork() is a particular case of spawn() that generates a new instance of a V8 engine. | Spawn() launches a new process with the available set of commands. |
Multiple workers run on a single node code base for multiple tasks. | This method doesn’t generate a new V8 instance, and only a single copy of the node module is active on the processor. |
41. What is WASI, and why is it being introduced?
42. What are the different types of HTTP requests?
GET:
Used to retrieve the dataPOST
Generally used to make a change in state or reactions on the serverHEAD:
Similar to the GET method, but asks for the response without the response bodyDELETE:
Used to delete the predetermined resource43. Explain the concept of middleware in Node.js.
1) Execute any code
2) Update or modify the request and the response objects
3) Finish the request-response cycle
4) Invoke the next middleware in the stack
44. What is fork in node JS?
45. For Node.js, why does Google use the V8 engine?
46. How would you connect a MongoDB database to Node.js?
1) Start by creating a MongoClient object
2) Specify a connection URL with the correct IP address and the name of the database you want to create
var mongoclient = required('mongodb').mongoclient;
var url = "mongodp://localhost:27017/mydp";
mongoclient.connect(url,function(err,dp){
if(err) throw err;
console.log("database created!");
db.close();
});
47. Describe Node.js exit codes.
1) Uncaught fatal exception
2) Unused
3) Fatal Error
4) Internal Exception handler Rub-time failure.