Coding Interviews

Most Asked Node.js and Express.js Interview Questions

Updated: Mar 15, 2026
Most Asked Node.js and Express.js Interview Questions

1. What is Node.js?

Node.js is a runtime environment that allows JavaScript to be executed on the server side. It uses the Chrome V8 engine, is written in C++, and is known for non-blocking, event-driven architecture.

2. What is Express.js?

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for building web and mobile applications. It simplifies the process of creating server-side logic and handling HTTP requests and responses. Key Features of Express.js: Middleware support: Allows handling of requests in a layered approach. Routing: Simplifies the creation of route handlers for different HTTP methods and URLs. Integration: Easily integrates with databases (MongoDB, MySQL, etc.) and template engines (EJS, Pug, etc.). Lightweight: Does not enforce strict project structure or components. Scalable: Ideal for building both small apps and large-scale enterprise-level APIs.

3. Why use Express.js with Node.js?

Node.js provides a runtime, but Express.js adds structure and utilities to easily handle routing, request parsing, middleware, and more.

4. What is the V8 Engine?

The V8 engine is Google’s open-source JavaScript engine used in the Chrome browser and Node.js. It is written in C++ and is responsible for compiling and executing JavaScript code. Key Features of V8: High Performance: V8 compiles JavaScript to native machine code using Just-In-Time (JIT) compilation, making it very fast. Used in Node.js: Node.js uses V8 to run JavaScript code outside the browser, enabling serverside scripting. Memory Management: V8 includes a garbage collector to manage memory automatically. Standalone: Although it powers Chrome, it can be embedded in other C++ applications. How V8 Works:

  1. Parses JavaScript code into an Abstract Syntax Tree (AST).
  2. Compiles the AST into bytecode.
  3. Uses JIT compilation to convert frequently-used code into machine code for faster execution.

5. What are the types of modules Node.js?

Built-in Module Local Modules (which we built) Third-Party Modules (these are like external packages or library created by the community)

6. Explain the role of fs module in Node.js and name some of its functions?

The fs module (short for File System) in Node.js provides a way to interact with the file system on your computer. It allows you to perform operations like reading, writing, updating, deleting, and renaming files and directories, both synchronously and asynchronously. Function Description

fs.readFile() Reads the contents of a file asynchronously.
fs.readFileSync() Reads a file synchronously (blocking).
fs.writeFile() Writes data to a file asynchronously (creates or replaces the file).
fs.writeFileSync() Synchronously writes data to a file.

7. Explain the role of http module in Node.js?

The http module in Node.js is a core module that allows you to create and manage HTTP servers and clients. It enables Node.js to handle incoming HTTP requests and send HTTP responses, making it fundamental for building web servers without using any external libraries. ##8. What is routing in express.js? Routing is the process of directing incoming HTTP requests to the appropriate handler functions based on the requests method (e.g, Get, Post) and the url path.

8. What is purpose of next() function in express.js middleware?

The next() function is used in middleware to pass control to the next middleware function in the stack. It is typically called at the end of a middleware function.

9. What is body-parser?

Body-parser is a middleware module for Express.js used to parse the incoming request body before your route handlers. When a client (like a browser or Postman) sends data in an HTTP request—especially in a POST, PUT, or PATCH request—that data is typically sent in the body of the request in raw format (e.g., JSON or URL-encoded string). body-parser converts this raw data into a usable JavaScript object and attaches it to req.body so that you can easily access it in your route handlers.

10. What Security machanisms are available in node?

Helmet.js (HTTP Header Security) Middleware for Express that sets various HTTP headers to protect against common vulnerabilities. Helps prevent: XSS attacks Clickjacking Rate Limiting Prevents brute-force attacks by limiting repeated requests from the same IP. Authentication & Authorization Use secure methods for user auth: JWT (JSON Web Token) OAuth2 Session-based auth

11. What are the key features of Node.js?

Asynchronous Event-driven Architecture Fast execution with V8 engine Single-threaded with event loop No buffering Built-in modules Cross-Platform

12. What is the difference between synchronous and asynchronous code?

Synchronous: Blocks the execution until the operation completes. Asynchronous: Executes operations in the background without blocking.

13. What is the Event Loop in Node.js?

An event loop is a programming mechanism that continuously monitors and dispatches events or tasks in a software application. Node.js uses the event loop to manage asynchronous operations efficiently by delegating tasks and using callbacks.

13. How does Node.js handle concurrency?

Node.js handles concurrency using its single-threaded, non-blocking event loop. It can manage many operations at once by delegating tasks and using callbacks without waiting for each one to finish.

14. What is the role of package.json?

It stores metadata about the project (name, version, scripts, dependencies) and is essential for managing packages using npm. It is the backbone of a Node.js project.

15. What is the difference between npm and Yarn?

Yarn was created by Facebook to improve speed and reliability. Yarn offers better caching and parallel installs. npm comes pre-installed with Node.js, Yarn needs manual installation.

16. What is a callback hell?

When callbacks are nested within other callbacks, it leads to difficult-to-maintain code, known as callback hell.

17. What is the difference between req.params and req.body?

req.params is used to access route parameters in the URL. req.body is used to access data sent in the body of a POST request.

18. What is middleware in Express.js/Node.js?

Middleware refers to functions that execute during the request-response cycle. They have access to req, res, and next(), and can modify or end the request.

19. What is Express.js?

Express.js is a lightweight and flexible Node.js web application framework for building APIs and web servers.

19. How do you handle errors in Express?

Using middleware with four arguments:

app.use((err, req, res, next) => {
 res.status(500).send({ error: err.message });
});

20. How do you read and write files in Node.js?

Using the fs module:

fs.readFile('file.txt', 'utf8', callback);
fs.writeFile('file.txt', data, callback);

21. What is the difference between readFile and createReadStream()?

 readFile: Reads entire file into memory (for small files).  createReadStream: Reads data in chunks, better for large files.

22. What are streams in Node.js?

Streams handle continuous data transfer. Types include: Readable (e.g. fs.createReadStream) Writable (e.g. fs.createWriteStream) Duplex (both readable and writable) Transform (modifies data)

23. What is a buffer in Node.js?

A buffer is a temporary memory storage used to handle binary data, especially when working with streams.

24. What is the difference between process.nextTick() and setImmediate()?

process.nextTick(): Executes after the current operation. setImmediate(): Executes in the next iteration of the event loop.

24. What is CORS and how do you handle it in Express?

CORS (Cross-Origin Resource Sharing) allows resource sharing from different domains. Use the cors middleware:

const cors = require('cors');
app.use(cors());

25. How do you connect Node.js with MongoDB?

Using Mongoose:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/db');

26. What is Mongoose?

Mongoose is an ODM for MongoDB that helps structure data using schemas and models.

27. What are environment variables and how are they used?

Environment variables store sensitive configuration like DB URIs. Access them with process.env and manage with .env files and the dotenv package.

28. What is JWT and how is it used?

JWT is a token format used for stateless authentication. It encodes user data (like user ID, role, etc.) and is signed with a secret or private key. Example use case: A client logs in → gets a JWT → includes it in future requests via the Authorization header. Stateless: The server does not store session data for the client. Instead, all required data is stored inside the JWT itself. It contains three parts: header, payload, and signature. The server issues it to the client, and the client sends it on protected requests.

29. JWT is stateless, so once it's issued, you can't revoke it until it expires?

So if you create a token with a 30-day expiration, and the user logs in, you can't force them out after 15 days unless you add something extra — because the server doesn't track the token. Here are common strategies Token Blacklisting: When the user logs out or needs to be forcefully removed (after 15 days, for example), you store the token in a blacklist (e.g., Redis, database). On every request, check if the token is blacklisted Refresh Token: Issue an access token (expires in ~15 min) that will check on each request.

30. How do you implement authentication in Node.js?

JWT for token-based authentication Passport.js for session-based authentication

31. What are some ways to improve performance in a Node.js app?

Use clustering to utilize all CPU cores Optimize database queries Use caching (e.g., Redis) Avoid blocking operations

32. What is clustering in Node.js?

Clustering creates multiple processes of your app running on different CPU cores. If one crashes, others keep running.

33. What are HTTP methods in Node.js?

Common HTTP methods:  GET  POST  PUT  DELETE

34. What are function types in Node.js?

 Blocking: Waits for task completion (e.g., fs.readFileSync())  Non-blocking: Uses callbacks/promises (e.g., fs.readFile())

35. Name some built-in modules in Node.js.

 http  fs  url  path  os

36If a task, like sending an email, takes a long time (for example 10 seconds), how can we prevent the user from waiting for it and instead give them a success message immediately while processing the task in the background? How can this be achieved?

This can be handled using asynchronous background processing. Instead of performing the long-running task directly in the request-response cycle, the server can:

  1. Respond immediately to the user with a success message once the request is received.
  2. Process the long task asynchronously in the background. Common approaches include: Using job queues like Bull, RabbitMQ, or Kafka, where the task (e.g., sending an email) is added to the queue and processed by a worker separately. Background workers or cron jobs that handle tasks without blocking the main server thread.

Related Articles

Comments (0)

No comments yet. Be the first!