30-45 Express.js Interview Questions to Ace Your Interview (With Diagrams & MongoDB Section)

Here’s a neat, clean, and interactive HTML guide for revising Express.js concepts. Each question includes a simple explanation, code example with comments explaining each line, and a pro tip. We've added diagrams for key topics and a MongoDB section with 5 extra questions. Click the question titles to toggle content!

1. What’s Express.js in simple words?

Explanation: Express.js is like a helper for Node.js that makes building web apps or APIs super easy. It’s a framework that handles routing (directing users to the right page) and middleware (functions that process requests). It’s like a fast-food counter—quick, flexible, and gets the job done!

Code Example (if asked):


const express = require('express'); // Import Express framework
const app = express(); // Create an Express application instance
app.get('/', (req, res) => res.send('Hey, welcome!')); // Define a GET route for the root URL, sends a response
app.listen(3000, () => console.log('Server’s up at port 3000!')); // Start server on port 3000, log when ready
                

How to Explain Code: The code sets up a basic server. `require('express')` loads the framework, `express()` creates the app, `app.get()` handles requests to the root URL, and `app.listen()` starts the server on port 3000, enabling it to accept HTTP requests.

Pro Tip: Mention Express is lightweight and flexible, unlike raw Node.js, which is like cooking from scratch—Express saves time!

2. How do you install Express.js?

Explanation: You add Express to your project using npm, like downloading an app. It’s stored in your project’s folder for consistency.

Code Example (if asked):


npm init -y  # Initialize a new Node.js project with default package.json
npm install express  # Install Express framework to the project
                

How to Explain Code: `npm init -y` creates a `package.json` file to manage dependencies. `npm install express` adds Express to the project, saving it in `node_modules` and `package.json`.

Pro Tip: Use a specific version (e.g., `npm install express@4.18.2`) for production stability, like locking a recipe.

3. How do you set up a basic Express server?

Explanation: It’s like starting a food truck. Create an Express app, define routes, and listen on a port. Here’s a diagram of the HTTP request-response cycle:

HTTP Request-Response Cycle in Express.js

Code Example (if asked):


const express = require('express'); // Import Express framework
const app = express(); // Create an Express application instance
app.get('/', (req, res) => res.send('Welcome to my food truck!')); // Handle GET requests to root, send response
app.listen(3000, () => console.log('Truck’s open at http://localhost:3000')); // Start server on port 3000
                

How to Explain Code: `require('express')` loads Express, `express()` creates the app, `app.get()` defines a route for the root URL, and `app.listen()` starts the server on port 3000. The diagram shows how a client request reaches the server and gets a response.

Pro Tip: `app.listen` uses Node’s HTTP server, great for handling lots of requests.

4. What’s routing in Express?

Explanation: Routing is like a GPS for your app—it directs users to the right page based on URL and method (GET, POST). Here’s a routing diagram:

Express.js Routing Diagram Express.js Routing Flow Diagram

Code Example (if asked):


app.get('/home', (req, res) => res.send('You’re home!')); // Handle GET requests to /home, send response
app.post('/order', (req, res) => res.send('Order placed!')); // Handle POST requests to /order, send response
                

How to Explain Code: `app.get()` handles read requests for `/home`, sending a response. `app.post()` handles create requests for `/order`. The diagram shows how routes direct requests to controllers.

Pro Tip: Use patterns like `/user/:id` for dynamic URLs. Order specific routes before general ones.

5. What’s Middleware in Express?

🌐 Simple Meaning: Middleware in Express.js is a function that sits between the request and the response. It can check, modify, or log things before passing the request to the next part of the program.

🧠 Easy Example (Real Life): Imagine a security guard at the building gate. Before you enter, the guard checks your ID. In Express, middleware does the same—it checks or processes the request before it goes to the route.

⚙️ How It Works:

  • The client (browser) sends a request to the server.
  • The middleware receives it first—it can log, authenticate, or modify it.
  • After completing its job, it calls next() to pass control to the next middleware or route.
  • Finally, the route sends a response back to the client.

🧩 Common Uses of Middleware:

  • 🧾 Logging requests
  • 🔐 Checking authentication (login)
  • 🧰 Validating data
  • ⚠️ Handling errors
  • 📦 Serving static files
  • 🧮 Parsing JSON or form data

🧭 Middleware Chain Diagram: Middleware works in a chain—each one runs in order until next() is not called or a response is sent.

Express.js Middleware Chain Diagram

💻 Code Example:


const express = require('express');
const app = express();

// Middleware for all requests
app.use((req, res, next) => {
  console.log('Got a request for:', req.url); // Logs every URL
  next(); // Passes control to next handler
});

// Simple route
app.get('/', (req, res) => {
  res.send('Hello, this is the homepage!');
});

app.listen(3000, () => console.log('Server running on port 3000'));
    

📖 How to Explain This Code:

  • app.use() — adds a middleware for every request.
  • req — request object (details from client).
  • res — response object (used to send data back).
  • next() — moves to the next middleware or route.
  • Here, it simply logs the URL and moves on.

📊 Middleware Flow (Step by Step):

  1. Client sends request →
  2. Middleware runs →
  3. next() passes to next middleware →
  4. Route handler executes →
  5. Response sent to client ✅

💡 Pro Tip: Middleware is the backbone of Express. Use small, specific middlewares for logging, validation, and auth. Avoid putting heavy logic in global middleware—it can slow down your app.

🧾 Summary Table:

Step Action Who Does It
1 Request sent Client (Browser)
2 Request received & processed Middleware
3 Route executes Express Route
4 Response sent Server

✅ Final Line: Middleware = “Function that works in between request and response” — like a helper that prepares or checks everything before sending the final reply.

6. What does `next()` do in middleware?

Explanation: `next()` passes the request to the next middleware or route, like passing a plate to the next chef.

Code Example (if asked):


app.use((req, res, next) => { // Define middleware
  if (req.query.vip === 'true') next(); // If VIP query is true, proceed to next handler
  else res.send('Sorry, VIPs only!'); // Otherwise, send a response and stop
});
                

How to Explain Code: The middleware checks for a `vip` query parameter. If true, `next()` passes control; otherwise, it sends a response, halting the chain.

Pro Tip: `next('error')` jumps to error middleware. Use after `await` in async code.

7. How do you serve static files (like images or CSS)?

🌐 Simple Meaning: Static files are files that don’t change — like images, CSS, JavaScript, or HTML files. They are directly sent to the browser as they are.

🧁 Easy Example: Think of static files as ready-to-serve snacks — no cooking (processing) needed. The server just gives them to you when you ask.

⚙️ In Express.js: Use express.static() to tell Express which folder contains your static files.

💻 Code Example:


const express = require('express');
const app = express();

// Serve all files inside "public" folder
app.use(express.static('public'));

app.listen(3000, () => console.log('Server running on port 3000'));
    

📖 How to Explain Code:

  • express.static('public') means: “If someone requests a file, check inside the public folder.”
  • No need to create routes for images or CSS — Express handles it automatically.
  • Example: if there’s a file public/style.css, you can access it as http://localhost:3000/style.css.

🗂️ Folder Structure Example:


project/
│
├── public/
│   ├── style.css
│   ├── script.js
│   └── logo.png
│
└── app.js
    

💡 Pro Tip: Always use a folder like public for safety and organization. For faster loading, you can also use a CDN (Content Delivery Network) later.

✅ Summary:

  • 📦 Static files = fixed files (images, CSS, JS)
  • ⚙️ Use express.static() to serve them
  • 🪄 No routing needed — they’re auto-accessible
  • 🏁 Keep them in a “public” folder for best practice

8. What’s req.params?

🌐 Simple Meaning: req.params is used to get values from the URL when the URL has dynamic parts.

🧠 Easy Example: If someone visits /user/123, Express can capture 123 using req.params.id.

💻 Code Example:


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

📖 How It Works:

  • :id → defines a dynamic part of the URL.
  • req.params.id → reads that dynamic value.
  • Example: URL → /user/45 Output → “User ID: 45”

📎 Real-Life Example:

  • /product/10 → Show product 10
  • /student/5 → Show student with ID 5

💡 Pro Tip: Always validate or sanitize req.params before using them — to prevent hackers from sending unsafe data.

✅ Summary:

  • 📦 req.params → holds values from URL.
  • 🪄 Used when route has dynamic parts like :id.
  • ⚙️ Example: /user/:idreq.params.id
  • 🔒 Always validate for safety.

9. How do you send JSON in Express?

🌐 Simple Meaning: To send data from the server to the client in JSON format (used in APIs), use res.json().

🧠 Easy Example: JSON (JavaScript Object Notation) is just data in key–value pairs like: { "name": "Deepak", "age": 21 }.

💻 Code Example:


app.get('/api/menu', (req, res) => {
  res.json({ dish: 'Pizza', price: 10 });
});
    

📖 How It Works:

  • res.json() sends the response as JSON format.
  • It automatically sets the header Content-Type: application/json.
  • This is how most APIs send data to clients or apps.
  • Example Response: { "dish": "Pizza", "price": 10 }

⚙️ Compare:

  • res.send() → sends text, HTML, or data (general use).
  • res.json() → sends proper JSON + sets correct headers (for APIs).

📡 Real-Life Use: APIs use res.json() to send data like user info, products, weather, etc. Example: /api/users → sends list of users as JSON.

💡 Pro Tip: Always send a status code with API responses for clarity:


res.status(200).json({ message: 'Success', data: users });
    

✅ Summary:

  • 📦 res.json() → sends data as JSON.
  • 🧩 Used for APIs and data exchange.
  • ⚙️ Auto-sets JSON headers.
  • 🚀 Better than res.send() for structured data.
  • 💬 Add res.status() for better API responses.

10. What HTTP methods does Express support?

🌐 Simple Meaning: HTTP methods are the types of requests a client (like a browser or app) sends to the server. Express supports all major methods used in REST APIs.

📦 Common Methods in Express:

  • GET → Read data (e.g., show users)
  • POST → Create new data (e.g., add user)
  • PUT → Update entire data (e.g., replace user info)
  • PATCH → Update part of data (e.g., change user name only)
  • DELETE → Remove data (e.g., delete user)

💻 Code Example:


app.get('/users', (req, res) => res.send('Get all users'));
app.post('/users', (req, res) => res.send('Add new user'));
app.put('/users/:id', (req, res) => res.send('Update full user'));
app.patch('/users/:id', (req, res) => res.send('Update part of user'));
app.delete('/users/:id', (req, res) => res.send('Delete user'));
    

🧠 Easy Example (Real Life): Think of a restaurant:

  • GET → Look at menu 🍽️
  • POST → Place a new order 🧾
  • PUT → Replace your full order 🍕➡️🥗
  • PATCH → Change just one item in the order 🍕➡️🍔
  • DELETE → Cancel your order ❌

💡 Pro Tip: These methods form the base of REST APIs. Use correct methods for clarity and proper API design. Also, handle CORS (Cross-Origin Resource Sharing) if requests come from another domain.

✅ Summary:

  • ⚙️ Express supports: GET, POST, PUT, PATCH, DELETE
  • 🔁 Used to Create, Read, Update, Delete data (CRUD)
  • 🧩 Important for REST API design
  • 🌍 Handle CORS for external access

11. How do you handle a 404 (page not found)?

🌐 Simple Meaning: A 404 error happens when the client requests a page or route that doesn’t exist. Express can handle this with a catch-all middleware.

⚙️ How It Works:

  • Add a middleware at the end of all routes.
  • It catches all requests that don’t match existing routes.
  • Sets status code to 404 and sends a message or JSON.

💻 Code Example:


app.use((req, res) => {
  res.status(404).send('Oops, page not found!');
});
    

📖 How to Explain:

  • This middleware runs only if no other route matches.
  • res.status(404) sets the HTTP status to 404.
  • res.send() sends a user-friendly message.

💡 Pro Tip:

  • For APIs, return JSON instead of text: res.status(404).json({ error: 'Not found' })
  • Log 404 errors to track broken links or analytics.

✅ Summary:

  • 🛑 404 = page or route not found
  • ⚙️ Use catch-all middleware at the end
  • 📝 Send text or JSON to inform the client
  • 📊 Logging helps track issues

13. Full Express App Example (Router + Controller + Middleware + Model + DB)

🧠 Simple Meaning: A full Express app is built by separating the code into different parts — like dividing a restaurant into kitchen (controller), menu (routes), manager (middleware), and storage (database).

📁 Folder Structure:


project/
│
├── app.js                 # Main app file
├── routes/
│   └── users.js           # Router for user routes
├── controllers/
│   └── userController.js  # Controller with user logic
├── middleware/
│   └── logger.js          # Custom middleware
├── models/
│   └── User.js            # Mongoose model (MongoDB)
└── .env                   # Database connection config
    

⚙️ Step 1: Middleware (middleware/logger.js)


module.exports = (req, res, next) => {
  console.log(`[${new Date().toLocaleTimeString()}] ${req.method} ${req.originalUrl}`);
  next();
};
    

🧩 Step 2: User Model (models/User.js)


const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  age: Number,
}, { timestamps: true });

module.exports = mongoose.model('User', userSchema);
    

👨‍🍳 Step 3: Controller (controllers/userController.js)


const User = require('../models/User');

// CRUD operations
exports.getAllUsers = async (req, res) => { /*...*/ };
exports.getUserById = async (req, res) => { /*...*/ };
exports.createUser = async (req, res) => { /*...*/ };
exports.updateUser = async (req, res) => { /*...*/ };
exports.deleteUser = async (req, res) => { /*...*/ };
    

📌 Step 4: Router (routes/users.js)

Router Meaning: express.Router() is a “mini-app” that groups related routes together. You can add middleware to the router or specific routes.

Router Analogy: Main app = restaurant, Router = drinks menu, Router-level middleware = waiter checking all drink orders, Route = individual drink.


const express = require('express');
const router = express.Router();
const userController = require('../controllers/userController');

// Router-level middleware
router.use((req, res, next) => {
  console.log('User Router Middleware Triggered');
  next();
});

// Routes
router.get('/', userController.getAllUsers);
router.get('/:id', userController.getUserById);
router.post('/', userController.createUser);
router.put('/:id', userController.updateUser);
router.patch('/:id', userController.updateUser);
router.delete('/:id', userController.deleteUser);

module.exports = router;
    

🚀 Step 5: Main App (app.js)


const express = require('express');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const usersRouter = require('./routes/users');
const logger = require('./middleware/logger');

dotenv.config();
const app = express();

// Global middleware
app.use(express.json());
app.use(logger);

// Mount router
app.use('/users', usersRouter);

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

// Connect DB & start server
mongoose.connect(process.env.MONGO_URI)
  .then(() => app.listen(3000, () => console.log('Server running')))
  .catch(err => console.log(err));
    

🔑 How It Works (Flow):

  • 🟢 app.js → main app, global middleware, mounts routers
  • 🟠 users router → mini-app grouping all user routes, router-level middleware
  • 🔵 controller → handles logic for each route
  • 🟣 model → connects to MongoDB database
  • 🟤 middleware → logs or validates requests
  • ⚫ Catch-all 404 → handles unknown routes

📊 API Endpoints:

MethodRouteDescription
GET/usersGet all users
GET/users/:idGet user by ID
POST/usersCreate user
PUT/users/:idUpdate user
DELETE/users/:idDelete user

💬 Real-Life Analogy:

  • 🧾 app.js = Main restaurant manager
  • 📋 router = Menu section (e.g., Users menu)
  • 👨‍🍳 controller = Chef preparing each order
  • 🧱 model = Storage (database)
  • 🕵️‍♂️ middleware = Waiter/security guard checking requests

💡 Summary:

  • ✅ Use express.Router() to organize routes
  • ✅ Keep business logic in controllers
  • ✅ Use middleware for logging, validation, or security
  • ✅ Use Mongoose models to interact with MongoDB
  • ✅ Keep app modular, maintainable, and clean

13. How do you add middleware to a specific route?

🧠 Explanation: Middleware can be applied to a single route by placing it before the route handler. It runs only for that route, allowing conditional checks like authentication or logging.

💻 Code Example:


// Middleware function
const checkUser = (req, res, next) => {
  console.log('Checking user...');
  // Example condition
  if(req.query.user === 'admin') next();
  else res.status(403).send('Access denied!');
};

// Apply middleware to specific route
app.get('/secret', checkUser, (req, res) => {
  res.send('Secret menu!');
});
        

🔍 How to Explain: - `checkUser` runs before the route handler. - `next()` passes control to the handler if conditions pass. - If condition fails, middleware can stop the request and send a response.

Pro Tip: Use route-specific middleware for auth, logging, validation, or other checks. You can chain multiple middleware functions for one route.

14. What are the arguments of middleware functions?

🧠 Explanation: Middleware functions receive req, res, and next. - `req` → request info - `res` → response object - `next` → function to pass control to next middleware
Error-handling middleware also receives err as the first argument.

Pro Tip: `req` and `res` are mutable, so you can add data (like notes to an order) for later middleware or routes.

15. What’s the difference between app.use() and app.get()?

🧠 Explanation:

  • app.use() → Runs middleware for all HTTP methods and routes (global or path-specific)
  • app.get() → Handles only GET requests for a specific route

💡 Key Points:

  • Use app.use() for logging, authentication, or JSON parsing
  • Use app.get() (or app.post(), app.put()) for route-specific logic
  • app.use() can take a path to limit its scope, e.g., app.use('/users', middleware)

Pro Tip: app.use() = global tasks; app.get() = page/route-specific tasks.

16. How do you handle query parameters?

Explanation: Query parameters are extra info in a URL after a ?, like ?q=book&category=novel. You can access them in Express using req.query.

Code Example:


const express = require('express');
const app = express();
const port = 3000;

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

    res.send(`Searching for: ${searchTerm}, Category: ${category || 'all'}`);
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});
        

How to Explain: req.query.q gets the q value from the URL. Example: /search?q=booksearchTerm = 'book'. Similarly, req.query.category gets the category.

Pro Tip: Always validate query parameters to prevent security issues.

17. What’s the default port for Express?

Explanation: No strict default, but 3000 is common for dev.

Pro Tip: Use `process.env.PORT` for cloud platforms.

18. How is Node.js different from Express.js?

Explanation: Node.js is a JavaScript runtime that lets you run JS on the server. Express.js is a framework built on top of Node.js that provides tools and shortcuts to make building web applications easier and faster.

Example:


// Node.js alone
const http = require('http');

const server = http.createServer((req, res) => {
    res.write('Hello from Node.js!');
    res.end();
});

server.listen(3000, () => console.log('Server running on port 3000'));

// Using Express.js
const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('Hello from Express.js!'));

app.listen(3000, () => console.log('Server running on port 3000'));
        

Pro Tip: Express simplifies routing, middleware, and other common tasks, but for very simple apps, plain Node.js might be faster.

19. What are some cool features of Express.js?

Explanation: Express.js has many features that make building web apps easier:

  • Routing: Handle different URLs with different code.
  • Middleware: Functions that run before your routes to process requests.
  • Static Files: Serve images, CSS, JS files easily.
  • Template Engines: Use Pug, EJS, or Handlebars to render dynamic HTML.

Pro Tip: Express works seamlessly with async/await and can integrate easily with databases like MongoDB.

20. What’s a `.env` file for?

Explanation: Stores secrets like API keys. Load with `dotenv`.

Code Example (if asked):


require('dotenv').config(); // Load environment variables from .env file
app.listen(process.env.PORT || 3000); // Start server on PORT from .env or default to 3000
                

How to Explain Code: `dotenv.config()` loads variables from `.env`. `process.env.PORT` uses the defined port or falls back to 3000.

Pro Tip: Part of 12-factor apps, keeps secrets safe.

21. What are JWTs?

Explanation: JWTs (JSON Web Tokens) are secure tokens used to authenticate and authorize users in web applications. They are stateless, meaning you don’t need to store sessions on the server.

Structure of a JWT:

  • Header: Contains metadata like token type and algorithm (e.g., HMAC SHA256).
  • Payload: Contains user data or claims (e.g., userId, role).
  • Signature: Encodes the header and payload with a secret key to prevent tampering.

Flow from Backend to Frontend:

  1. User logs in and provides credentials (username/password).
  2. Backend verifies credentials and generates a JWT using jwt.sign().
  3. Backend sends the JWT to the frontend (usually in the response body or as a cookie).
  4. Frontend stores the token (in localStorage or cookies).
  5. Frontend sends the JWT in the Authorization header for protected routes: Authorization: Bearer <token>.
  6. Backend verifies the token using jwt.verify() before granting access.

Code Example (Backend):


const express = require('express');
const jwt = require('jsonwebtoken');
const app = express();
app.use(express.json());

const SECRET_KEY = 'mysecret';

// Login route
app.post('/login', (req, res) => {
    const { username, password } = req.body;

    // Dummy check (replace with DB check)
    if(username === 'user' && password === 'pass') {
        const token = jwt.sign({ userId: 1, username }, SECRET_KEY, { expiresIn: '1h' });
        return res.json({ token });
    }

    res.status(401).send('Invalid credentials');
});

// Protected route
app.get('/dashboard', (req, res) => {
    const authHeader = req.headers['authorization'];
    const token = authHeader && authHeader.split(' ')[1];

    if(!token) return res.status(401).send('Token missing');

    try {
        const user = jwt.verify(token, SECRET_KEY);
        res.send(`Welcome ${user.username} to your dashboard!`);
    } catch(err) {
        res.status(403).send('Invalid token');
    }
});

app.listen(3000, () => console.log('Server running on port 3000'));
        

How to Explain Code:

  • jwt.sign(payload, secret, options) → Creates a token with user info and expiration.
  • jwt.verify(token, secret) → Validates the token and extracts user info.
  • Frontend sends the token in the Authorization header for protected routes.

Pro Tips:

  • Always keep your secret key safe.
  • Use HTTPS to prevent token theft.
  • Set token expiration to reduce risks.
  • Use express-jwt or middleware to protect routes efficiently.

22. What’s Bcrypt for?

Explanation: Hashes passwords securely with salt.

Code Example (if asked):


const bcrypt = require('bcrypt'); // Import bcrypt for password hashing
bcrypt.hash('myPassword', 10, (err, hash) => { /* save hash */ }); // Hash password with 10 salt rounds
                

How to Explain Code: `bcrypt.hash()` creates a secure password hash with 10 rounds of salting, stored for later verification.

Pro Tip: Higher rounds are safer but slower.

23. Why separate the Express app and server?

Explanation: Keep app (routes, middleware) and server (`app.listen`) separate.

Code Example (if asked): (app.js)


const express = require('express'); // Import Express framework
const app = express(); // Create Express app instance
app.get('/', (req, res) => res.send('Hello!')); // Define root route
module.exports = app; // Export app for use in other files
                

(server.js):


const app = require('./app'); // Import the Express app
app.listen(3000); // Start server on port 3000
                

How to Explain Code: `app.js` defines the app and routes, exported via `module.exports`. `server.js` imports and starts the server with `app.listen()`.

Pro Tip: Helps testing and scaling.

24. What’s ESLint?

Explanation: Checks code for mistakes and consistency.

Pro Tip: Use with `eslint-plugin-node` for Express rules.

25. What’s the difference between `res.send()` and `res.json()`?

Explanation: `res.send()` sends anything; `res.json()` sends JSON.

Pro Tip: `res.json()` is better for APIs, handles edge cases.

26. What’s scaffolding in Express?

Explanation: Creates starter project with `express-generator`.

Code Example (if asked):


npm install -g express-generator // Install express-generator globally
express my-cool-app // Generate a new Express project structure
                

How to Explain Code: Installs `express-generator` and creates a project with folders and files for quick setup.

Pro Tip: Customize for your needs.

27. What’s CORS in Express?

Explanation: Allows cross-origin data sharing.

Code Example (if asked):


const cors = require('cors'); // Import CORS middleware
app.use(cors({ origin: 'http://mywebsite.com' })); // Enable CORS for specified origin
                

How to Explain Code: `cors` middleware allows requests from `mywebsite.com`, enabling cross-origin API calls.

Pro Tip: Configure origins for security. Handle preflight requests.

28. What are built-in middlewares in Express?

Explanation: Tools like `express.json()`, `express.urlencoded()`, `express.static()`.

Pro Tip: Replaced `body-parser` in Express 4+.

29. How do you set app settings in Express?

Explanation: Use `app.set(name, value)` for settings.

Code Example (if asked):


app.set('view engine', 'pug'); // Set Pug as the template engine
app.set('views', './views'); // Set the folder for template files
                

How to Explain Code: `app.set()` configures the app to use Pug for templates and look for them in the `views` folder.

Pro Tip: Use `app.get(name)` to check settings.

30. Which template engines work with Express?

Explanation: Supports Pug, EJS, Handlebars.

Pro Tip: Pug is clean; templates help SEO.

31. How do you send plain HTML in Express?

Explanation: Use `res.send()` or `res.sendFile()`.

Code Example (if asked):


app.get('/', (req, res) => res.send('<h1>Hello World!</h1>')); // Send raw HTML response
                

How to Explain Code: `res.send()` sends an HTML string directly to the client for the root URL.

Pro Tip: Use templates or caching for efficiency.

32. What’s `res.cookie()` for?

Explanation: Sets cookies in the browser.

Code Example (if asked):


res.cookie('username', 'John', { maxAge: 900000, httpOnly: true }); // Set a cookie with name, value, and options
                

How to Explain Code: `res.cookie()` sets a cookie named `username` with value `John`, expiring in 900000ms, and `httpOnly` for security.

Pro Tip: Use `httpOnly`, `secure` for safety.

33. What’s the Pug template engine?

Explanation: Pug writes HTML with indentation.

Code Example (if asked): (app.js)


app.set('view engine', 'pug'); // Set Pug as the template engine
app.get('/', (req, res) => res.render('index', { title: 'My App' })); // Render index.pug with title data
                

(index.pug):


doctype html // Declare HTML5 document
html
  head
    title= title // Use title variable from render
  body
    h1 Welcome! // Static HTML heading
                

How to Explain Code: In `app.js`, `app.set()` configures Pug, and `res.render()` renders `index.pug` with data. The `.pug` file uses indentation to define HTML structure.

Pro Tip: Pug’s mixins make reusable HTML easy.

34. How do you handle errors in Express?

Explanation: Use middleware with four arguments (`err, req, res, next`). Here’s an error handling flowchart:

Express.js Error Handling Flowchart

Code Example (if asked):


app.use((err, req, res, next) => { // Error middleware with four parameters
  console.error(err); // Log the error for debugging
  res.status(500).send('Something broke!'); // Send 500 status and error message
});
                

How to Explain Code: This middleware catches errors, logs them, and sends a 500 response. The diagram shows how errors flow from routes to this handler.

Pro Tip: Log with Winston, return user-friendly messages.

35. How do you parse JSON data from requests?

Explanation: Use `express.json()` for POST JSON data.

Code Example (if asked):


app.use(express.json()); // Parse incoming JSON requests
app.post('/data', (req, res) => res.send(`Got: ${req.body.name}`)); // Handle POST, access JSON body
                

How to Explain Code: `express.json()` parses JSON payloads into `req.body`. The POST route accesses `name` from the body and responds.

Pro Tip: Set size limit (`{ limit: '1mb' }`) for safety.

36. What’s the difference between `req.params`, `req.query`, and `req.body`?

Explanation:

  • `req.params`: From URL paths (e.g., `/user/123` → `req.params.id`).
  • `req.query`: From queries (e.g., `?name=John` → `req.query.name`).
  • `req.body`: From POST/PUT data.

Pro Tip: Use `params` for IDs, `query` for filters, `body` for payloads. Secure `body` with HTTPS.

37. How do you add authentication in Express?

Explanation: Use middleware like Passport.js or JWT for login checks.

Code Example (if asked):


const jwt = require('express-jwt'); // Import express-jwt for JWT validation
app.use('/secure', jwt({ secret: 'mykey', algorithms: ['HS256'] })); // Protect /secure routes with JWT
                

How to Explain Code: `express-jwt` middleware checks for a valid JWT in request headers, protecting `/secure` routes.

Pro Tip: Combine JWT with bcrypt, rate-limit attacks.

38. What’s Helmet in Express?

Explanation: Adds security headers to protect against attacks.

Code Example (if asked):


const helmet = require('helmet'); // Import Helmet for security headers
app.use(helmet()); // Apply security headers to all responses
                

How to Explain Code: `helmet()` adds headers like XSS protection to all responses for security.

Pro Tip: Customize headers for specific needs.

39. How do you use environment variables in Express?

Explanation: Store settings in `.env`, load with `dotenv`.

Code Example (if asked):


require('dotenv').config(); // Load environment variables from .env file
const dbUrl = process.env.DB_URL; // Access DB_URL from environment
                

How to Explain Code: `dotenv.config()` loads `.env` variables, and `process.env.DB_URL` accesses the database URL securely.

Pro Tip: Part of 12-factor apps, keeps secrets safe.

40. What’s Morgan in Express?

Explanation: Logs HTTP requests for debugging.

Code Example (if asked):


const morgan = require('morgan'); // Import Morgan for request logging
app.use(morgan('tiny')); // Log requests in 'tiny' format (method, URL, status)
                

How to Explain Code: `morgan('tiny')` logs request details like method and URL for debugging.

Pro Tip: Use `tiny` for dev, `combined` for prod. Pair with Winston.

MongoDB Integration with Express.js

Added 5 questions on MongoDB for deeper understanding.

41. How do you connect MongoDB to Express.js?

Explanation: Use `mongoose` to connect to MongoDB. Here’s a connection flow diagram:

Express.js MongoDB Connection Diagram

Code Example (if asked):


const mongoose = require('mongoose'); // Import Mongoose for MongoDB interaction
mongoose.connect('mongodb://localhost:27017/mydb', { useNewUrlParser: true }) // Connect to MongoDB database 'mydb'
  .then(() => console.log('Connected to MongoDB')) // Log success
  .catch(err => console.error('Connection error', err)); // Log connection errors
                

How to Explain Code: `mongoose.connect()` establishes a connection to the MongoDB database at the specified URL. Promises handle success or failure. The diagram shows the flow from Express to MongoDB.

Pro Tip: Use environment variables for DB URL. Handle reconnection in production.

42. What is Mongoose?

Explanation: Mongoose is an ODM for MongoDB, providing schema-based data modeling.

Pro Tip: Translates JS objects to MongoDB documents, simplifies NoSQL.

43. How to define a schema in Mongoose?

Explanation: Schemas define document structure. Here’s a schema diagram:

Mongoose Schema Diagram

Code Example (if asked):


const mongoose = require('mongoose'); // Import Mongoose
const userSchema = new mongoose.Schema({ // Define schema for User
  name: { type: String, required: true }, // Name field, required string
  age: Number // Age field, optional number
});
const User = mongoose.model('User', userSchema); // Create User model from schema
                

How to Explain Code: `mongoose.Schema` defines the structure of User documents with fields and types. `mongoose.model()` creates a model for database operations. The diagram shows Mongoose’s role in structuring data.

Pro Tip: Add validators for data integrity.

44. How to perform CRUD operations with Mongoose in Express?

Explanation: Use model methods for Create, Read, Update, Delete.

Code Example (if asked):


// Create
app.post('/users', async (req, res) => { // Handle POST to create user
  const user = new User(req.body); // Create new User instance from request body
  await user.save(); // Save to database
  res.send(user); // Send saved user as response
});
// Read: await User.find(); // Find all users
// Update: await User.updateOne({ _id: id }, { $set: { name: 'New' } }); // Update user by ID
// Delete: await User.deleteOne({ _id: id }); // Delete user by ID
                

How to Explain Code: The POST route creates a user with `User` model, saves it, and responds. Other operations use `find()`, `updateOne()`, and `deleteOne()` for CRUD functionality.

Pro Tip: Use try-catch, populate() for relationships.

45. How to handle MongoDB errors in Express?

Explanation: Catch MongoDB errors with Express error middleware.

Code Example (if asked):


app.post('/users', async (req, res, next) => { // Handle POST to create user
  try { // Try block for error handling
    const user = new User(req.body); // Create new User from request body
    await user.save(); // Save to database
    res.send(user); // Send saved user
  } catch (err) { // Catch any errors
    next(err); // Pass error to error middleware
  }
});
                

How to Explain Code: The POST route tries to save a user. If an error occurs (e.g., validation), `catch` passes it to `next()` for the error middleware to handle.

Pro Tip: Log specific errors (e.g., DuplicateKey) with Winston.

How to Impress in Your Interview

With these notes, code, and visuals, you’re ready to shine! Practice explaining in your own words.