NestJS Introduction 5 — Questions and Answers
Question 1: How does NestJS support both REST and GraphQL in the same project?
- They cannot coexist in NestJS
- By using platform adapters — REST via controllers, GraphQL via @nestjs/graphql module (Correct answer)
- By running two separate servers
- By converting REST endpoints to GraphQL resolvers automatically
Correct answer: By using platform adapters — REST via controllers, GraphQL via @nestjs/graphql module
NestJS has @nestjs/graphql which integrates Apollo or Mercurius, allowing GraphQL resolvers to coexist with REST controllers.
Question 2: What is 'lazy loading' of modules in NestJS used for?
- Loading modules only when a specific route is hit to improve startup time (Correct answer)
- Loading environment variables lazily
- Deferring database connections
- Loading TypeScript types at runtime
Correct answer: Loading modules only when a specific route is hit to improve startup time
Lazy loading modules with LazyModuleLoader reduces application startup time by initializing modules on demand rather than at boot.
Question 3: Which decorator is used to inject a value by a custom token rather than a class type?
- @InjectToken()
- @Inject(TOKEN) (Correct answer)
- @UseToken()
- @ProvideWith()
Correct answer: @Inject(TOKEN)
@Inject(TOKEN) allows injecting custom providers registered with a string or Symbol token instead of a class reference.
Question 4: What is a 'global module' in NestJS and how is it declared?
- A module that auto-imports all other modules; declared with @AllModules()
- A module whose exported providers are available everywhere without importing; declared with @Global() (Correct answer)
- A module mounted at the root path; declared in main.ts
- A module with no imports array
Correct answer: A module whose exported providers are available everywhere without importing; declared with @Global()
@Global() on a module makes its exports available application-wide so other modules don't need to import it explicitly.
Question 5: What does the 'controllers' array in @Module() declare?
- Services that handle business logic
- Classes that handle incoming HTTP requests and define routes (Correct answer)
- Middleware functions
- Database entity classes
Correct answer: Classes that handle incoming HTTP requests and define routes
Controllers listed in the 'controllers' array are instantiated by NestJS and their route handlers are registered with the HTTP adapter.
Question 6: Which NestJS lifecycle hook is called once the application is fully started and listening?
- onModuleInit()
- onApplicationBootstrap() (Correct answer)
- onModuleDestroy()
- beforeApplicationShutdown()
Correct answer: onApplicationBootstrap()
onApplicationBootstrap() is called after all modules have been initialized and the application is listening for connections.
Question 7: What advantage does NestJS provide over using plain Express for large applications?
- NestJS is faster than Express at runtime
- NestJS enforces architectural structure, TypeScript, and built-in DI which reduces boilerplate and improves maintainability (Correct answer)
- NestJS removes the need for a database
- NestJS eliminates the need for environment variables
Correct answer: NestJS enforces architectural structure, TypeScript, and built-in DI which reduces boilerplate and improves maintainability
NestJS imposes clear conventions (modules, controllers, services) and built-in dependency injection, making large codebases more organized and testable.
How does NestJS support both REST and GraphQL in the same project?