Consider an Express app running in production mode using the default error handler. What will the user see if an error occurs?
const express = require('express'); const app = express(); app.get('/', (req, res) => { throw new Error('Oops!'); }); app.listen(3000);
Think about what Express does by default when no custom error handler is set.
By default, Express's error handler shows detailed error stack traces only in development mode. In production, it shows a generic message. However, if NODE_ENV is not set to 'production', the detailed error is shown even in production.
In Express, what is the effect of calling app.disable('x-powered-by') in a production setup?
const express = require('express'); const app = express(); app.disable('x-powered-by'); app.get('/', (req, res) => { res.send('Hello'); }); app.listen(3000);
Think about what the 'X-Powered-By' header does in HTTP responses.
The 'X-Powered-By' header reveals the server technology (Express). Disabling it helps reduce information leakage to attackers.
Choose the correct way to serve static files efficiently in production with Express.
Think about how to enable browser caching for static files.
Setting maxAge to '1d' tells browsers to cache static files for one day, improving performance in production.
Given this Express app code, why does it crash in production but works fine in development?
const express = require('express'); const app = express(); app.get('/', (req, res) => { setTimeout(() => { throw new Error('Delayed error'); }, 100); res.send('Hello'); }); app.listen(3000);
Think about how Express handles errors thrown inside asynchronous code.
Errors thrown inside asynchronous callbacks like setTimeout are not caught by Express error handlers and cause the Node.js process to crash unless handled explicitly.
Why must you set the environment variable NODE_ENV to 'production' when deploying an Express app?
Consider what changes Express makes based on NODE_ENV.
Setting NODE_ENV to 'production' tells Express to run in optimized mode, disabling detailed error messages and enabling performance improvements.