NestJS Basics — Series 2

Preview — 3 of 10 questions

What is main.ts responsible for in a standard NestJS project?

javascript
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
AIt declares the application's controllers and providers; AppModule only holds configuration values
BIt is the entry point: it builds the application instance from the root module and starts the HTTP server listening on a port
CIt is compiled into a service that Nest injects into every controller automatically
DIt runs once per incoming HTTP request, rebuilding the application for each one

What does this command do?

javascript
nest generate service users
# or, shorthand:
nest g s users
AIt creates users.service.ts plus a spec file, and registers the service in the nearest module's providers array
BIt only prints the file contents to the terminal so you can copy them manually
CIt creates a service and immediately deploys it as a standalone microservice
DIt rewrites every existing controller to inject the new service

What status codes do these two handlers return on success, by default?

javascript
@Controller('cats')
export class CatsController {
  @Get()
  findAll() { return []; }

  @Post()
  create() { return { id: 1 }; }
}
ABoth return 200 OK, because Nest applies one default status to every route
BBoth return 204 No Content unless a body is explicitly returned
CGET returns 200 OK and POST returns 201 Created
DNest returns 500 until you add an explicit @HttpCode() decorator

Sign up free to play

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