Versioning & Streaming

Preview — 3 of 10 questions

You call app.enableVersioning({ type: VersioningType.URI }). What URL shape does a @Version('1') controller now serve?

javascript
import { VersioningType } from '@nestjs/common';
// main.ts
app.enableVersioning({ type: VersioningType.URI }); // default prefix 'v'

@Controller({ path: 'users', version: '1' })
export class UsersV1Controller {} // serves /v1/users
A/users with header X-Version: 1
B/users with media type application/vnd.api+json;v=1
C/users?version=1
D/v1/users

What does @Res({ passthrough: true }) allow that a plain @Res() does not?

javascript
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller('auth')
export class AuthController {
  @Get('me')
  me(@Res({ passthrough: true }) res: Response) {
    res.cookie('seen', '1'); // side effect on raw response
    return { user: 'alice' }; // still serialized by Nest
  }
}
AIt lets you read the request body twice
BIt lets you use the raw response (e.g. set a cookie) while still returning a value for Nest to serialize
CIt disables all interceptors for the handler
DIt automatically streams the response

When would you inject @Next() into a route handler?

javascript
import { Controller, Get, Next, Res } from '@nestjs/common';
import { NextFunction, Response } from 'express';

@Controller('legacy')
export class LegacyController {
  @Get()
  handle(@Res() res: Response, @Next() next: NextFunction): void {
    if (!res.locals.ready) return next(); // defer to next middleware
    res.send('done');
  }
}
ATo pass control to the next Express middleware/handler in the chain
BTo call the next route handler in NestJS's controller list
CTo advance an RxJS Observable
DTo trigger the next lifecycle hook

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.