Bird
Raised Fist0
Expressframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of third-party middleware in an Express app?
easy
A. To add extra features like parsing or logging easily
B. To replace the Express framework completely
C. To write HTML pages automatically
D. To create a database connection

Solution

  1. Step 1: Understand middleware role

    Middleware adds extra functions to Express apps, like parsing data or logging requests.
  2. Step 2: Compare options

    Replacing Express or creating databases are not middleware tasks; middleware enhances features.
  3. Final Answer:

    To add extra features like parsing or logging easily -> Option A
  4. Quick Check:

    Middleware adds features = A [OK]
Hint: Middleware adds features, not replaces core framework [OK]
Common Mistakes:
  • Thinking middleware replaces Express
  • Confusing middleware with database tools
  • Assuming middleware generates HTML pages
2. Which code snippet correctly installs and uses the cors middleware in an Express app?
easy
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);

Solution

  1. Step 1: Check import style

    Modern Express apps use ES modules, so import cors from 'cors'; is correct.
  2. Step 2: Check middleware usage

    Middleware must be called as a function: app.use(cors()); not just cors.
  3. Final Answer:

    import cors from 'cors'; app.use(cors()); -> Option C
  4. Quick Check:

    Import + call middleware function = C [OK]
Hint: Import middleware and call it with () in app.use [OK]
Common Mistakes:
  • Forgetting parentheses after middleware name
  • Mixing require and import syntax
  • Using app.use(cors) without calling it
3. What will be the output when this Express app code runs and receives a JSON POST request?
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
app.use(bodyParser.json());
app.post('/data', (req, res) => {
  res.send(req.body.name);
});
app.listen(3000);
medium
A. The server responds with an empty string
B. The server responds with '[object Object]'
C. The server crashes with a syntax error
D. The server responds with the value of 'name' from the JSON body

Solution

  1. Step 1: Understand bodyParser.json()

    This middleware parses JSON request bodies and sets req.body to the parsed object.
  2. Step 2: Check response code

    The route sends back req.body.name, so the response is the 'name' value from JSON.
  3. Final Answer:

    The server responds with the value of 'name' from the JSON body -> Option D
  4. Quick Check:

    bodyParser parses JSON, response sends req.body.name = B [OK]
Hint: bodyParser.json() parses JSON, so req.body has data [OK]
Common Mistakes:
  • Not using bodyParser.json(), so req.body is undefined
  • Expecting raw JSON string instead of parsed object
  • Sending req.body instead of req.body.name
4. Identify the error in this Express app code using helmet middleware:
import express from 'express';
import helmet from 'helmet';
const app = express();
app.use(helmet);
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);
medium
A. helmet is imported incorrectly
B. helmet middleware is not called as a function
C. app.listen is missing a callback
D. app.get route is missing a response

Solution

  1. Step 1: Check middleware usage

    Middleware must be called as a function: app.use(helmet()); not app.use(helmet);.
  2. Step 2: Verify other code parts

    Import is correct, app.listen works without callback, and route sends response properly.
  3. Final Answer:

    helmet middleware is not called as a function -> Option B
  4. Quick Check:

    Middleware must be called with () = A [OK]
Hint: Always call middleware with () in app.use [OK]
Common Mistakes:
  • Using app.use(helmet) instead of app.use(helmet())
  • Thinking app.listen needs a callback
  • Forgetting to send response in route
5. You want to add morgan logging middleware only for POST requests in your Express app. Which code correctly applies this?
hard
A. app.use((req, res, next) => { if(req.method === 'POST') morgan('dev')(req, res, next); else next(); });
B. app.post('*', morgan('dev'));
C. app.use(morgan('dev')); if(req.method === 'POST') next();
D. app.use('/post', morgan('dev'));

Solution

  1. Step 1: Understand middleware conditional use

    To run middleware only on POST, wrap it in a function checking req.method.
  2. Step 2: Analyze options

    app.use((req, res, next) => { if(req.method === 'POST') morgan('dev')(req, res, next); else next(); }); calls morgan only if method is POST, else calls next(). Others either apply to wrong paths or misuse middleware.
  3. Final Answer:

    app.use((req, res, next) => { if(req.method === 'POST') morgan('dev')(req, res, next); else next(); }); -> Option A
  4. Quick Check:

    Conditional middleware call for POST = D [OK]
Hint: Wrap middleware in function checking req.method for selective use [OK]
Common Mistakes:
  • Using app.use with path that doesn't match POST method
  • Calling middleware without next() in else branch
  • Trying to use app.post with middleware incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard