NestJS Technology & Digital Applications 5 — Questions and Answers
Question 1: How does NestJS support versioning for REST API endpoints?
- Only via URL path prefixes manually set on controllers
- Via URI, header, media type, or custom versioning strategies built into the framework (Correct answer)
- Via package.json version field
- By creating separate modules per version
Correct answer: Via URI, header, media type, or custom versioning strategies built into the framework
NestJS has built-in API versioning supporting URI (v1/v2), header-based, media-type, and custom strategies configured at the application level.
Question 2: What does the `@Render()` decorator do in NestJS?
- Serializes the response as JSON
- Renders an HTML template view using the configured template engine (Correct answer)
- Converts a class to a render-safe DTO
- Enables server-side rendering for React components
Correct answer: Renders an HTML template view using the configured template engine
@Render() tells the controller method to render a named template file using the configured view engine (e.g., Handlebars, Pug).
Question 3: In NestJS, what is the difference between `useGlobalPipes()` and providing a pipe with `APP_PIPE`?
- They are identical in behavior
- useGlobalPipes() is set outside DI context so the pipe cannot inject dependencies; APP_PIPE works within DI (Correct answer)
- APP_PIPE only applies to WebSocket gateways
- useGlobalPipes() supports async pipes while APP_PIPE does not
Correct answer: useGlobalPipes() is set outside DI context so the pipe cannot inject dependencies; APP_PIPE works within DI
useGlobalPipes() registers a pipe outside the DI container, so the pipe can't use dependency injection; using APP_PIPE token inside a module allows full DI support.
Question 4: Which decorator in NestJS is used to set custom HTTP response headers?
- @Header() (Correct answer)
- @SetHeader()
- @ResponseHeader()
- @HttpHeader()
Correct answer: @Header()
@Header(name, value) sets a custom response header on the HTTP response for that route handler.
Question 5: What role does `Reflector` play in NestJS guards and interceptors?
- It reflects TypeScript types at runtime for validation
- It reads metadata set by decorators like @SetMetadata() from handlers and classes (Correct answer)
- It mirrors the request object for logging purposes
- It provides a proxy to the underlying Express/Fastify instance
Correct answer: It reads metadata set by decorators like @SetMetadata() from handlers and classes
Reflector is a helper class injected into guards/interceptors to read custom metadata attached via @SetMetadata() or other metadata decorators.
Question 6: Which NestJS feature enables sharing a single WebSocket server instance across multiple gateways?
- Declaring gateways in the same module
- Using a shared ServerOptions config object
- Setting the same port and namespace in @WebSocketGateway() across gateways (Correct answer)
- Importing WebSocketModule.forRoot() once
Correct answer: Setting the same port and namespace in @WebSocketGateway() across gateways
Multiple gateways configured with the same port in @WebSocketGateway() share the same underlying WebSocket server instance created by the adapter.
Question 7: What is the purpose of `@nestjs/swagger`'s `@ApiProperty()` decorator?
- Validates properties using OpenAPI schema rules
- Documents DTO properties in the auto-generated Swagger/OpenAPI spec (Correct answer)
- Marks a property as required in the database schema
- Exposes a class property as a public API endpoint
Correct answer: Documents DTO properties in the auto-generated Swagger/OpenAPI spec
@ApiProperty() annotates DTO class properties so that @nestjs/swagger can include them with correct types and descriptions in the generated OpenAPI documentation.
How does NestJS support versioning for REST API endpoints?