Bird
0
0

Which of the following is a correct way to enable CORS globally in a NestJS application?

easy📝 Conceptual Q2 of 15
NestJS - Middleware
Which of the following is a correct way to enable CORS globally in a NestJS application?
Aapp.use(cors());
Bapp.set('cors', true);
Capp.enableCors();
Dapp.apply(cors()).forRoutes('*');
Step-by-Step Solution
Solution:
  1. Step 1: Recall NestJS global CORS enabling method

    In NestJS, the built-in method to enable CORS globally is app.enableCors().
  2. Step 2: Understand other options

    Using app.use(cors()) is possible but less idiomatic. app.set('cors', true) and app.apply(cors()).forRoutes('*') are incorrect for global CORS enabling.
  3. Final Answer:

    app.enableCors(); -> Option C
  4. Quick Check:

    Global CORS = app.enableCors() [OK]
Quick Trick: Use app.enableCors() for global CORS in NestJS [OK]
Common Mistakes:
  • Using app.use(cors()) instead of app.enableCors()
  • Trying to set CORS with app.set() method
  • Misusing app.apply() for global middleware

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes