Express.js Quiz ( Intermediate ) - All Questions
This intermediate-level Express.js quiz is designed for developers who already know the basics and want to test applied, real-world concepts. It focuses on middleware flow, routing patterns, error handling, REST APIs, and Express internals commonly asked in interviews.
Question 1: What is the main purpose of Express middleware?
- Render UI
- Handle database queries
- Process requests before sending a response
- Compile JavaScript
Explanation: Middleware functions execute during the request-response lifecycle.
Question 2: In which order does Express execute middleware?
- Alphabetical order
- Random order
- Order of definition
- Based on priority flags
Explanation: Middleware executes in the order it is defined.
Question 3: What happens if next() is not called in middleware?
- Server crashes
- Request hangs
- Response auto-sends
- Next route runs
Explanation: Without next(), the request lifecycle stops.
Question 4: Which middleware signature is used for error handling?
- (req, res)
- (req, res, next)
- (err, req, res, next)
- (error, res)
Explanation: Error middleware has four parameters.
Question 5: Where should error-handling middleware be placed?
- At the top
- After routes
- Before app.listen()
- Inside routers only
Explanation: Error middleware must be defined after routes.
Question 6: What does app.use('/api', router) do?
- Creates API automatically
- Mounts router at /api path
- Limits HTTP methods
- Adds middleware globally
Explanation: It mounts all router paths under /api.
Question 7: Which method is best for updating an entire resource?
Explanation: PUT replaces the entire resource.
Question 8: Which method is best for partial updates?
Explanation: PATCH updates part of a resource.
Question 9: What does req.headers contain?
- Query parameters
- Request body
- HTTP headers
- Route params
Explanation: req.headers stores HTTP header information.
Question 10: How can you send a custom status code with JSON?
- res.json(400)
- res.send(400)
- res.status(400).json()
- res.code(400).send()
Explanation: res.status().json() sends status with JSON.
Question 11: What is the purpose of express.Router()?
- Start server
- Create modular route handlers
- Parse JSON
- Handle errors
Explanation: Routers help organize routes into modules.
Question 12: What does req.params return?
- Query strings
- POST data
- Route parameters
- Headers
Explanation: req.params contains route parameters.
Question 13: What is the difference between req.params and req.query?
- Both are same
- params from URL path, query from URL string
- query from body, params from headers
- params only for POST
Explanation: params come from route path, query from URL string.
Question 14: Which middleware enables Cross-Origin Resource Sharing?
Explanation: cors enables CORS in Express.
Question 15: Why is helmet commonly used?
- Performance optimization
- Logging
- Securing HTTP headers
- Routing
Explanation: Helmet improves security by setting headers.
Question 16: Which status code indicates successful creation?
Explanation: 201 indicates a resource was created.
Question 17: Which status code indicates no content?
Explanation: 204 means success with no response body.
Question 18: How do you handle async errors properly in Express?
- try-catch only
- Callbacks only
- Pass error to next()
- Ignore errors
Explanation: Passing errors to next() triggers error middleware.
Question 19: Which pattern is best for structuring Express apps?
- Single file
- MVC pattern
- Inline handlers
- Global variables
Explanation: MVC improves maintainability.
Question 20: What does express.json() replace?
- fs module
- body-parser json middleware
- cors
- helmet
Explanation: express.json() replaces body-parser for JSON.
Question 21: Which HTTP method should be idempotent?
Explanation: PUT is idempotent by design.
Question 22: What is the role of next(err)?
- Stops server
- Skips remaining middleware
- Triggers error handler
- Restarts request
Explanation: Passing err invokes error middleware.
Question 23: Which Express method handles all HTTP verbs for a route?
- app.any()
- app.use()
- app.all()
- app.route()
Explanation: app.all() handles all HTTP methods.
Question 24: What does res.sendStatus(404) do?
- Sends JSON
- Sends status only
- Redirects request
- Throws error
Explanation: sendStatus sets status and sends its message.
Question 25: Why should validation middleware run early?
- Better UI
- Fail fast on bad requests
- Faster routing
- Improved SEO
Explanation: Early validation prevents unnecessary processing.
Question 26: Which object stores environment variables?
- req.env
- process.env
- app.env
- config.env
Explanation: process.env stores environment variables.
Question 27: What does app.param() handle?
- Query params
- Route parameters preprocessing
- Body parsing
- Headers
Explanation: app.param runs logic for route parameters.
Question 28: Which status code indicates a client error?
Explanation: 4xx codes represent client errors.
Question 29: What happens if two routes match a request?
- Last route runs
- All routes run
- First matching route runs
- Server crashes
Explanation: Express executes the first matching route.
Question 30: Which Express feature supports REST API versioning?
- Query params
- URL prefixing
- Headers only
- Cookies
Explanation: Versioning is commonly done using URL prefixes.
Question 31: Why should you avoid heavy logic in route handlers?
- SEO issues
- Poor readability and scalability
- Routing errors
- Security flaws
Explanation: Heavy logic should be moved to services.
Question 32: Which middleware parses form-data?
- express.json()
- multer
- cors
- helmet
Explanation: Multer handles multipart/form-data.
Question 33: What does res.set() do?
- Sets response body
- Sets HTTP headers
- Sets route params
- Sets cookies
Explanation: res.set() sets response headers.
Question 34: Which response method ends the response?
- res.send()
- res.end()
- res.json()
- res.redirect()
Explanation: res.end() ends the response.
Question 35: What is the default content type of res.json()?
- text/plain
- text/html
- application/xml
- application/json
Explanation: res.json() sets application/json.
Question 36: Why should Express apps be stateless?
- Simpler routing
- Better horizontal scaling
- Faster rendering
- Cleaner syntax
Explanation: Stateless apps scale more easily.
Question 37: Which method is used to serve static assets?
- app.assets()
- app.public()
- express.static()
- res.static()
Explanation: express.static serves static files.
Question 38: What does req.originalUrl provide?
- Base URL only
- Modified URL
- Full original request URL
- Query string only
Explanation: originalUrl contains the full request path.
Question 39: Which Express feature improves maintainability?
- Inline logic
- Router separation
- Global state
- Hardcoded values
Explanation: Separating routers improves maintainability.
Question 40: Which middleware should handle authentication?
- After response
- Before protected routes
- Inside controllers only
- In database layer
Explanation: Auth middleware must run before protected routes.
Question 41: Which HTTP status code indicates conflict?
Explanation: 409 indicates a conflict.
Question 42: What does res.redirect(301) mean?
- Temporary redirect
- Permanent redirect
- Unauthorized
- Server error
Explanation: 301 indicates permanent redirect.
Question 43: Which Express pattern avoids callback nesting?
- Nested middleware
- Async/await
- Global handlers
- Inline callbacks
Explanation: Async/await improves readability.
Question 44: Which layer should handle business logic?
- Routes
- Controllers or services
- Middleware only
- Views
Explanation: Business logic belongs in controllers or services.
Question 45: What is the primary goal of Express.js?
- Frontend rendering
- Minimal and flexible backend framework
- Database management
- Mobile app development
Explanation: Express is minimal and flexible for backend APIs.