Bird
Raised Fist0
Node.jsframework~10 mins

Third-party middleware usage in Node.js - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Express middleware package.

Node.js
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Ahttp
Bpath
Cfs
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using core Node.js modules like 'http' instead of 'express'.
Misspelling the package name.
2fill in blank
medium

Complete the code to use the middleware function in the Express app.

Node.js
app.[1](middlewareFunction);
Drag options to blanks, or click blank then click option'
Aget
Blisten
Cuse
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using route methods like 'get' or 'post' instead of 'use'.
Trying to call 'listen' to add middleware.
3fill in blank
hard

Fix the error in the middleware function signature.

Node.js
function middleware(req, [1], next) {
  console.log('Middleware running');
  next();
}
Drag options to blanks, or click blank then click option'
Aresponse
Bres
Cnext
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' as the second parameter instead of the third.
Using incorrect parameter names.
4fill in blank
hard

Fill both blanks to import and use the 'cors' middleware in Express.

Node.js
const [1] = require('cors');
app.[2]([1]());
Drag options to blanks, or click blank then click option'
Acors
Blisten
Cuse
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'listen' instead of 'use' to apply middleware.
Importing the wrong package.
5fill in blank
hard

Fill all three blanks to import, configure, and use the 'helmet' middleware with options.

Node.js
const [1] = require('helmet');
const helmetOptions = { contentSecurityPolicy: false };
app.[2]([1]([3]));
Drag options to blanks, or click blank then click option'
Ahelmet
Buse
ChelmetOptions
Dcors
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'cors' with 'helmet'.
Not passing options to the middleware function.

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