Interview Questions

Get ready for your next interview with our comprehensive question library

Express.js Interview Questions

Filter by Difficulty

1.

How do you create a basic Express.js application?

beginner
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
  res.send('Hello World!');
});
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

This creates a basic server that listens on port 3000 and responds with "Hello World!" when accessing the root route.

2.

What is middleware in Express.js?

beginner

Middleware functions are functions that have access to the request object (req), response object (res), and the next middleware function in the application's request-response cycle. Middleware can execute code, modify req/res objects, end the request-response cycle, or call the next middleware.

// Custom middleware
app.use((req, res, next) => {
  console.log('Time:', Date.now());
  next(); // Call next to continue to the next middleware
});
3.

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

beginner
  1. Application-level middleware - Bound to app object using app.use()
  2. Router-level middleware - Bound to router object using router.use()
  3. Error-handling middleware - Takes four arguments (err, req, res, next)
  4. Built-in middleware - Provided by Express (express.static, express.json)
  5. Third-party middleware - From npm packages (morgan, cors, helmet)
4.

How do you handle different HTTP methods in Express.js?

beginner
app.get('/users', (req, res) => {
  res.send('GET request');
});
app.post('/users', (req, res) => {
  res.send('POST request');
});
app.put('/users/:id', (req, res) => {
  res.send('PUT request');
});
app.delete('/users/:id', (req, res) => {
  res.send('DELETE request');
});
5.

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

beginner

The next() function passes control to the next middleware function. If not called, the request will be left hanging. You can pass an error to next(err) to trigger error handling middleware.

app.use((req, res, next) => {
  if (req.headers.authorization) {
    next(); // Continue to next middleware
  } else {
    next(new Error('Unauthorized')); // Trigger error handling
  }
});
6.

How do you serve static files in Express.js?

beginner

Use the built-in express.static middleware:

// Serve files from 'public' directory
app.use(express.static('public'));
// Serve with virtual path prefix
app.use('/static', express.static('public'));
7.

What is the difference between `req.params`, `req.query`, and `req.body`?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
8.

How do you enable CORS in Express.js?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
9.

What is Express Router and why use it?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
10.

How do you implement error handling in Express.js?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
11.

What is the difference between `app.use()` and `app.get()`?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
12.

How do you implement authentication middleware?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
13.

How do you handle file uploads in Express.js?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
14.

What are Express.js route parameters and how do you validate them?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
15.

How do you implement rate limiting in Express.js?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

What is the purpose of body-parser middleware?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

How do you implement session management in Express.js?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What is the difference between cookies and sessions?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
19.

How do you implement logging in Express.js?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
20.

How do you handle environment variables in Express.js?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 44 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime