Bird
0
0

In a NestJS application, how should you correctly import and apply the Helmet middleware in main.ts?

easy📝 Syntax Q3 of 15
NestJS - Middleware
In a NestJS application, how should you correctly import and apply the Helmet middleware in main.ts?
Aimport helmet from 'helmet'; app.use(helmet);
Bimport * as helmet from 'helmet'; app.use(helmet);
Cconst helmet = require('helmet'); app.use(helmet);
Dimport helmet from 'helmet'; app.use(helmet());
Step-by-Step Solution
Solution:
  1. Step 1: Import Helmet correctly

    Use ES module default import: import helmet from 'helmet';
  2. Step 2: Apply Helmet as middleware

    Call the function to get middleware: app.use(helmet());
  3. Final Answer:

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

    Helmet must be called as a function to return middleware [OK]
Quick Trick: Always call helmet() when applying middleware [OK]
Common Mistakes:
  • Using app.use(helmet) without parentheses
  • Importing helmet incorrectly with require or * as
  • Not calling helmet as a function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes