0
0
Node.jsframework~20 mins

Third-party middleware usage in Node.js - 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.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.