Bird
0
0

Consider this code snippet in main.ts:

medium📝 Debug Q6 of 15
NestJS - Middleware
Consider this code snippet in main.ts:
import helmet from 'helmet';
app.use(helmet);

What is the issue with this middleware usage?
AHelmet requires additional configuration options to work
BHelmet is imported incorrectly and will cause a runtime error
CHelmet middleware should be applied after app.listen()
DHelmet middleware is not invoked as a function, so no middleware is applied
Step-by-Step Solution
Solution:
  1. Step 1: Understand Helmet usage

    Helmet exports a function that returns middleware, so it must be called.
  2. Step 2: Identify the problem

    Using app.use(helmet); passes the function itself, not the middleware it returns.
  3. Step 3: Correct usage

    Use app.use(helmet()); to apply the middleware properly.
  4. Final Answer:

    Helmet middleware is not invoked as a function, so no middleware is applied -> Option D
  5. Quick Check:

    Middleware functions must be invoked before passing to app.use() [OK]
Quick Trick: Call helmet() to get middleware before app.use() [OK]
Common Mistakes:
  • Passing helmet function instead of calling it
  • Assuming import style causes error
  • Thinking middleware order is the issue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes