1. What is clustering in Node.js/Express?
AdvancedAnswer: Both b and c
Clustering spawns multiple worker processes to utilize all CPU cores. Each worker runs a separate Node.js instance. Improves performance and reliability.
24 questions that come up in Express.js technical interviews, each with the answer and an explanation of why it is right.
Test yourself — 90 question bankAnswer: Both b and c
Clustering spawns multiple worker processes to utilize all CPU cores. Each worker runs a separate Node.js instance. Improves performance and reliability.
Answer: A minimal and flexible Node.js web application framework
Express.js is a fast, unopinionated, minimalist web framework for Node.js. It provides a robust set of features for building web and mobile applications.
Answer: Both b and c
Express Router is a complete middleware and routing system. It allows you to create modular, mountable route handlers in separate files.
Answer: npm install express
Express is installed using npm (Node Package Manager) with the command `npm install express`. You can also use yarn: `yarn add express`.
Answer: express.Router()
Create a router using `const router = express.Router()`. Then define routes on router and export it to be used with app.use().
Answer: All of the above
Use built-in cluster module. Master process forks workers equal to CPU cores. Incoming connections distributed across workers using round-robin.
Answer: All of the above
Route middleware runs only for specific routes. Pass middleware as arguments before the handler: `app.get('/path', middleware1, middleware2, handler)`
Answer: require('express')()
Create an Express app by requiring the module and calling it as a function: `const express = require('express'); const app = express();`
Answer: Both b and c
PM2 is a production process manager. Features: cluster mode, auto-restart on crash, log management, monitoring, zero-downtime deployments.
Answer: All of the above
Error-handling middleware has four parameters. Define it after all other middleware. It catches errors passed to next(err) or thrown in async code.
Answer: app.listen()
The `app.listen(port, callback)` method binds and listens for connections on the specified port. Example: `app.listen(3000, () => console.log('Server running'))`
Answer: All of the above
Graceful shutdown completes in-flight requests before stopping. Listen for SIGTERM/SIGINT signals, close server, close DB connections, then exit.
Answer: All of the above
Streaming processes data in chunks without loading entire content in memory. Essential for large files. Use Node.js streams with pipe() method.
Answer: A URL path that maps to a handler function
A route determines how an application responds to a client request at a specific endpoint (URI) and HTTP method. Example: `app.get('/users', handler)`
Answer: next(error)
Pass errors by calling `next(error)`. For synchronous code, you can throw, but it must be caught. For async code, always use next(error).
Answer: GET
GET method is used to retrieve data from the server. It should not modify server state and is idempotent (same result for multiple identical requests).
Answer: Both b and c
Template engines generate HTML dynamically by combining templates with data. Popular choices: EJS, Pug (formerly Jade), Handlebars, Mustache.
Answer: All of the above
Caching stores frequently accessed data to reduce database load and improve response time. Use Redis, Memcached, or in-memory caches like node-cache.
Answer: All of the above
Use Redis client library. Create cache middleware that checks Redis before executing handler. Set appropriate TTL (Time To Live) for cached data.
Answer: app.get(path, handler)
Define a GET route using `app.get(path, handler)`. Example: `app.get('/users', (req, res) => { res.send('Users list'); })`
Answer: app.set('view engine', 'ejs')
Set template engine using `app.set('view engine', 'engineName')`. Also set views directory: `app.set('views', './views')`
Answer: All of the above
Connection pooling maintains a pool of reusable database connections. Reduces overhead of creating/destroying connections. Configure pool size based on load.
Answer: Both a and b
Route handlers receive request (req) and response (res) objects. req contains request data, res is used to send the response back to the client.
Answer: res.render(view, data)
`res.render(view, data)` renders a view template with data and sends the resulting HTML to the client. View file extension can be omitted.
The full Express.js bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.
Take the Express.js quiz