0
0
Expressframework~20 mins

Third-party middleware installation in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when using this middleware setup?
Consider this Express app code snippet. What will be the response when a GET request is made to '/'?
Express
import express from 'express';
import helmet from 'helmet';

const app = express();

app.use(helmet());

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

app.listen(3000);
AThe response will be 'Hello World' with added security headers from helmet.
BThe response will be a 404 error because helmet blocks all routes by default.
CThe server will crash because helmet is not configured properly.
DThe response will be 'Hello World' but without any security headers.
Attempts:
2 left
💡 Hint
Helmet adds security headers but does not block routes.
📝 Syntax
intermediate
2:00remaining
Which option correctly installs and uses the 'cors' middleware?
You want to enable Cross-Origin Resource Sharing (CORS) in your Express app using the 'cors' package. Which code snippet correctly installs and applies the middleware?
A
const cors = require('cors');
app.use(cors());
B
import cors from 'cors';
app.use(cors());
C
import cors from 'cors';
app.use(cors);
D
const cors = require('cors');
app.use(cors);
Attempts:
2 left
💡 Hint
Use ES module import and call the middleware function.
🔧 Debug
advanced
2:00remaining
Why does this middleware not run as expected?
Given this Express app code, why does the custom middleware not log requests?
Express
import express from 'express';

const app = express();

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

app.use((req, res, next) => {
  console.log('Request received');
  next();
});

app.listen(3000);
AThe middleware function is not called because app.use is incorrect.
BThe middleware is missing the 'next' parameter, so it blocks the request.
CThe app.listen call is missing a callback, so middleware is skipped.
DThe middleware is placed after the route, so it never runs for '/' requests.
Attempts:
2 left
💡 Hint
Middleware order matters in Express.
state_output
advanced
2:00remaining
What is the output of this middleware chain?
Analyze this Express app code. What will be the response body when a GET request is made to '/'?
Express
import express from 'express';

const app = express();

app.use((req, res, next) => {
  req.customValue = 5;
  next();
});

app.use((req, res, next) => {
  req.customValue += 10;
  next();
});

app.get('/', (req, res) => {
  res.send(`Value is ${req.customValue}`);
});

app.listen(3000);
A"Value is 10"
B"Value is 5"
C"Value is 15"
DUndefined, because req.customValue is not set
Attempts:
2 left
💡 Hint
Middleware can add and modify request properties.
🧠 Conceptual
expert
2:00remaining
Which statement about third-party middleware installation is true?
Select the correct statement about installing and using third-party middleware in Express.
AThird-party middleware must be installed via npm and imported before use in the app.
BThird-party middleware can be used without installation if the package name is known.
CMiddleware functions must always be placed after route handlers to work correctly.
DExpress automatically installs popular middleware packages when detected in code.
Attempts:
2 left
💡 Hint
Think about how Node.js packages work.