Posted in

Top 30 Express.js Interview Questions and Answers for All Experience Levels

Prepare for your Express.js interview with these 30 carefully curated questions covering basic, intermediate, and advanced concepts. This guide helps freshers, candidates with 1-3 years, and 3-6 years of experience master Express.js fundamentals, practical implementation, and real-world scenarios.

Basic Express.js Interview Questions

1. What is Express.js?

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It simplifies handling HTTP requests, routing, middleware, and response rendering.[1][3][4]

2. What is the conventional port number used for Express.js applications?

Express.js does not have a fixed default port, but developers conventionally use port 3000 for development servers.[4]

const express = require('express');
const app = express();
app.listen(3000, () => {
  console.log('Server running on port 3000');
});

3. How do you create a basic Express.js application?

Create an Express app instance, define routes, add middleware, and start the server with app.listen().[3]

const express = require('express');
const app = express();
app.use(express.json());
app.get('/', (req, res) => res.send('Hello World'));
app.listen(3000);

4. What are HTTP methods supported in Express.js?

Express.js supports standard HTTP methods: GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS. GET retrieves data, POST submits data, PUT updates completely, PATCH updates partially, and DELETE removes resources.[3][4]

5. How do you parse JSON request bodies in Express.js?

Use the built-in express.json() middleware to parse incoming JSON requests with Content-Type application/json.[3]

app.use(express.json());
app.post('/data', (req, res) => {
  console.log(req.body);
  res.json({ received: true });
});

6. What are route parameters in Express.js?

Route parameters are dynamic URL segments defined with a colon (:) that capture values from the URL path.[2][4]

app.get('/users/:id', (req, res) => {
  const userId = req.params.id;
  res.send(`User ID: ${userId}`);
});

7. How do you access query parameters in Express.js?

Query parameters are available in req.query object for GET requests with ?key=value format.[2]

app.get('/search', (req, res) => {
  const { q, category } = req.query;
  res.json({ search: q, category });
});

8. What is express.static middleware used for?

The express.static middleware serves static files like images, CSS, and JavaScript directly from a directory.[4]

app.use(express.static('public'));

9. How do you install Express.js in a project?

Run npm install express to add Express.js as a dependency to your Node.js project.[3]

10. What are the key features of Express.js?

Key features include routing, middleware support, HTTP utility methods, static file serving, and template engine integration.[4]

Intermediate Express.js Interview Questions

11. What is middleware in Express.js?

Middleware functions have access to request (req), response (res), and next() function to pass control to the next middleware.[1][3]

app.use((req, res, next) => {
  console.log('Request logged');
  next();
});

12. What is the purpose of the next() function in middleware?

next() passes control to the next middleware function in the stack. Omitting next() hangs the request.[2][5]

13. How do you create custom middleware in Express.js?

Define a function with (req, res, next) signature and use app.use() to register it globally or for specific routes.[1]

14. What are the different types of middleware in Express.js?

Types include application-level (app.use()), router-level, error-handling (four arguments), built-in, and third-party middleware.[4]

15. How do you handle CORS in Express.js?

Use cors middleware or set Access-Control-Allow-Origin headers in responses to enable cross-origin requests.[5]

16. Explain error-handling middleware signature.

Error-handling middleware uses four arguments: (err, req, res, next) and must be defined after all routes.[1]

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

17. How do you prevent “Cannot set headers after they are sent” error?

Use return after res.send(), ensure single response path, and avoid calling next() after response is sent.[1]

18. What is the execution order of middleware in Express.js?

Middleware executes in the order defined. Routes mounted before middleware skip it, so order placement matters.[1]

19. How do you serve static files from multiple directories?

Chain multiple express.static calls in order of precedence.[4]

app.use(express.static('public'));
app.use(express.static('uploads'));

20. How do you implement authentication middleware?

Create middleware that checks auth tokens and calls next() if valid or sends 401 if invalid.[1]

Advanced Express.js Interview Questions

21. How do you prevent blocking the Node.js event loop in Express routes?

Offload CPU-intensive work to worker threads, child processes, or external queues to avoid blocking other requests.[1]

22. Scenario: At Zoho, authentication middleware skips some routes. How do you debug?

Check middleware registration order, route mounting sequence, and ensure next() is called conditionally.[1]

23. Why separate Express app creation from server listening?

Enables easier testing with supertest, cleaner deployment for serverless, and better code organization.[3]

const app = express();
// ... routes
module.exports = app;

24. Scenario: Multiple async operations in a route cause double responses at Paytm. Fix it.

Ensure only one response path executes using flags, promises.allSettled(), or early returns.[1]

25. How do you implement rate limiting in Express.js?

Use express-rate-limit middleware to restrict requests per IP or user within time windows.

26. Explain Express.js request-response lifecycle.

Middleware stack executes sequentially; matching route handler processes request; response sent finalizes cycle.[1]

27. Scenario: At Salesforce, 404 errors need custom handling. Implement it.

Define 404 middleware after all routes, then error-handling middleware for 500 errors.[5]

app.use((req, res) => {
  res.status(404).json({ error: 'Route not found' });
});

28. How do you optimize Express.js for production at Atlassian scale?

Enable clustering, compression middleware, helmet for security headers, and proper logging with Morgan.[3]

29. What are RESTful principles for Express.js routes?

Use appropriate HTTP methods: GET for retrieval, POST for creation, PUT/PATCH for updates, DELETE for removal.[2]

30. Scenario: At Adobe, heavy computation blocks requests. Solution?

Move CPU-intensive tasks to background jobs using Bull Queue or child_process.fork() to maintain responsiveness.[1]

Leave a Reply

Your email address will not be published. Required fields are marked *