Bird
Raised Fist0
Node.jsframework~20 mins

Third-party middleware usage in Node.js - 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.js server code using a third-party middleware. What will the server respond with when a GET request is made to /hello?
Node.js
import express from 'express';
import helmet from 'helmet';

const app = express();

app.use(helmet());

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

app.listen(3000);
AThe server responds with 'Hello World!' and includes security headers added by helmet.
BThe server responds with 'Hello World!' but no additional headers are added.
CThe server throws an error because helmet is not compatible with Express.
DThe server responds with a 404 Not Found error.
Attempts:
2 left
💡 Hint
Helmet adds security headers automatically when used as middleware.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses the 'cors' middleware in Express?
You want to enable CORS for all routes in your Express app using the 'cors' package. Which code snippet correctly applies the middleware?
A
import cors from 'cors';
app.use(cors);
B
import cors from 'cors';
app.use(cors());
C
const cors = require('cors');
app.use(cors);
D
const cors = require('cors');
app.use(cors());
Attempts:
2 left
💡 Hint
Use the middleware function returned by calling cors()
🔧 Debug
advanced
2:00remaining
Why does this middleware not run for the POST request?
Given this Express app code, why does the middleware not log the message when a POST request is made to /submit?
Node.js
import express from 'express';
const app = express();

app.use('/get', (req, res, next) => {
  console.log('Middleware triggered');
  next();
});

app.post('/submit', (req, res) => {
  res.send('Submitted');
});

app.listen(3000);
AThe middleware is not attached to POST requests, only GET requests.
BThe middleware is missing the next() call, so it never passes control.
CThe middleware only runs for routes starting with '/get', so it does not run for '/submit'.
DThe app.listen call is missing a callback, so middleware is not registered.
Attempts:
2 left
💡 Hint
Check the path prefix used in app.use for the middleware.
❓ state_output
advanced
2:00remaining
What is the order of console logs when using multiple middleware?
Consider this Express app code with two middleware functions and one route handler. What is the order of console logs when a GET request is made to /test?
Node.js
import express from 'express';
const app = express();

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

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

app.get('/test', (req, res) => {
  console.log('Route handler');
  res.send('Done');
});

app.listen(3000);
AFirst middleware -> Second middleware -> Route handler
BRoute handler -> First middleware -> Second middleware
CSecond middleware -> First middleware -> Route handler
DFirst middleware -> Route handler -> Second middleware
Attempts:
2 left
💡 Hint
Middleware runs in the order they are added before the route handler.
🧠 Conceptual
expert
2:00remaining
Which statement about third-party middleware in Express is TRUE?
Select the correct statement about using third-party middleware in Express.js applications.
AMiddleware functions cannot be asynchronous and must always complete synchronously.
BThird-party middleware must always be added after all route handlers to work properly.
COnce a middleware sends a response, the next middleware in the chain will still execute automatically.
DMiddleware functions can modify the request and response objects before passing control to the next middleware or route handler.
Attempts:
2 left
💡 Hint
Think about how middleware can change request or response data.

Practice

(1/5)
1. What is the main purpose of using third-party middleware in a Node.js Express app?
easy
A. To replace the Express framework completely
B. To add extra features without writing them from scratch
C. To write all server code manually
D. To make the app run without Node.js

Solution

  1. Step 1: Understand middleware role

    Middleware adds features like logging or parsing to an app without extra coding.
  2. Step 2: Compare options

    Only To add extra features without writing them from scratch correctly describes middleware's purpose; others are incorrect or unrelated.
  3. Final Answer:

    To add extra features without writing them from scratch -> Option B
  4. Quick Check:

    Middleware adds features [OK]
Hint: Middleware adds features without extra coding [OK]
Common Mistakes:
  • Thinking middleware replaces Express
  • Believing middleware runs without Node.js
  • Confusing middleware with full app code
2. Which of the following is the correct way to add a third-party middleware called helmet to an Express app?
easy
A. use.app(helmet);
B. app.helmet();
C. helmet(app);
D. app.use(helmet());

Solution

  1. Step 1: Recall Express middleware syntax

    Middleware is added using app.use() with the middleware function called.
  2. Step 2: Check options

    Only app.use(helmet()); uses app.use(helmet()); correctly; others have wrong syntax.
  3. Final Answer:

    app.use(helmet()); -> Option D
  4. Quick Check:

    Middleware added with app.use() [OK]
Hint: Use app.use() with middleware function call [OK]
Common Mistakes:
  • Forgetting parentheses after middleware name
  • Using incorrect method names like app.helmet()
  • Passing middleware without calling it
3. Given this code snippet, what will be the output when a GET request is made to '/'?
const express = require('express');
const morgan = require('morgan');
const app = express();
app.use(morgan('tiny'));
app.get('/', (req, res) => {
  res.send('Hello World');
});
app.listen(3000);
medium
A. Throws an error because morgan is not used correctly
B. Responds with 'Hello World' but no logs appear
C. Logs request details in console and responds with 'Hello World'
D. Server crashes on start

Solution

  1. Step 1: Understand morgan middleware

    Morgan logs HTTP request info to the console when used with app.use().
  2. Step 2: Analyze code behavior

    GET '/' triggers morgan logging and sends 'Hello World' response.
  3. Final Answer:

    Logs request details in console and responds with 'Hello World' -> Option C
  4. Quick Check:

    morgan logs + response sent [OK]
Hint: Morgan logs requests when used with app.use() [OK]
Common Mistakes:
  • Assuming morgan changes response content
  • Thinking morgan causes errors if used like this
  • Ignoring middleware effect on logging
4. Identify the error in this code that tries to use the cors middleware:
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors);
app.get('/', (req, res) => res.send('OK'));
app.listen(3000);
medium
A. Missing parentheses when calling cors middleware
B. cors middleware is not installed
C. app.get() is missing a callback function
D. app.listen() is called without a port

Solution

  1. Step 1: Check middleware usage

    Middleware must be called as a function: cors(), not just cors.
  2. Step 2: Verify code

    Code uses app.use(cors); missing parentheses, causing error.
  3. Final Answer:

    Missing parentheses when calling cors middleware -> Option A
  4. Quick Check:

    Middleware must be called with () [OK]
Hint: Always call middleware as a function with () [OK]
Common Mistakes:
  • Passing middleware reference without ()
  • Assuming app.get() lacks callback
  • Forgetting to specify port in listen
5. You want to add both helmet for security and express.json() for parsing JSON bodies in your Express app. Which code correctly adds both middlewares in the right order?
hard
A. app.use(helmet()); app.use(express.json());
B. app.use(express.json); app.use(helmet);
C. app.use(helmet); app.use(express.json);
D. app.use(express.json()); app.use(helmet());

Solution

  1. Step 1: Understand middleware order importance

    Security middleware like helmet should run before body parsing for best protection.
  2. Step 2: Check middleware calls

    Both must be called as functions with parentheses; options B and C miss these, A has them but wrong order.
  3. Step 3: Confirm correct order and syntax

    app.use(helmet()); app.use(express.json()); calls helmet first, then express.json(), both correctly called.
  4. Final Answer:

    app.use(helmet()); app.use(express.json()); -> Option A
  5. Quick Check:

    Helmet before json, both called [OK]
Hint: Call middleware with () and order security before parsing [OK]
Common Mistakes:
  • Not calling middleware as functions
  • Placing parsing before security middleware
  • Mixing up middleware order
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