0
0
Expressframework~20 mins

Why production setup matters in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Production Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens if you use express's default error handler in production?

Consider an Express app running in production mode using the default error handler. What will the user see if an error occurs?

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

app.get('/', (req, res) => {
  throw new Error('Oops!');
});

app.listen(3000);
AThe user is redirected to a custom error page automatically.
BThe server crashes and stops responding to requests.
CThe user sees a detailed stack trace and error message in the browser.
DThe user sees a generic error message without stack trace details.
Attempts:
2 left
💡 Hint

Think about what Express does by default when no custom error handler is set.

state_output
intermediate
2:00remaining
What is the effect of disabling 'x-powered-by' header in Express production?

In Express, what is the effect of calling app.disable('x-powered-by') in a production setup?

Express
const express = require('express');
const app = express();
app.disable('x-powered-by');

app.get('/', (req, res) => {
  res.send('Hello');
});

app.listen(3000);
AIt removes the 'X-Powered-By' header from HTTP responses, hiding Express usage.
BIt disables all HTTP headers for security reasons.
CIt enables detailed error messages for debugging.
DIt forces HTTPS connections automatically.
Attempts:
2 left
💡 Hint

Think about what the 'X-Powered-By' header does in HTTP responses.

📝 Syntax
advanced
2:00remaining
Which middleware setup is correct for production static files in Express?

Choose the correct way to serve static files efficiently in production with Express.

Aapp.use(express.static('public', { maxAge: '1d' }));
Bapp.use(express.static('public', { cacheControl: false }));
Capp.use(express.static('public', { maxAge: 0 }));
Dapp.use(express.static('public', { etag: false }));
Attempts:
2 left
💡 Hint

Think about how to enable browser caching for static files.

🔧 Debug
advanced
2:00remaining
Why does this Express app crash in production but not in development?

Given this Express app code, why does it crash in production but works fine in development?

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

app.get('/', (req, res) => {
  setTimeout(() => {
    throw new Error('Delayed error');
  }, 100);
  res.send('Hello');
});

app.listen(3000);
AThe app crashes because res.send is called twice.
BThe app crashes because the port 3000 is already in use.
CUncaught errors in asynchronous callbacks crash the app; no error handler catches them.
DThe app crashes because setTimeout is not supported in production.
Attempts:
2 left
💡 Hint

Think about how Express handles errors thrown inside asynchronous code.

🧠 Conceptual
expert
2:00remaining
Why is setting NODE_ENV to 'production' critical in Express apps?

Why must you set the environment variable NODE_ENV to 'production' when deploying an Express app?

AIt automatically enables HTTPS and security headers.
BIt enables Express to optimize performance and disable verbose error messages.
CIt forces the app to use a production database connection string.
DIt disables all middleware except static file serving.
Attempts:
2 left
💡 Hint

Consider what changes Express makes based on NODE_ENV.