import {
BadRequestException,
Controller,
Get,
Header,
HttpCode,
Param,
Query,
Redirect,
Req,
} from '@nestjs/common';
import { Request } from 'express';
@Controller()
export class AppController {
@Header('Set-Cookie', 'cookie=apple')
@HttpCode(401)
@Get('he*lo/*')
getHello(@Req() req: Request ): string {
throw new BadRequestException('강제 에러');
console.log(req);
return 'hello';
}
@Redirect('http://127.0.0.1:5500/src/front/index.html', 301)
@Get('redirect')
redirect(@Query('id') id: string) {
console.log('요청 처리 후 리다이렉트');
if (id === 'nest') {
return { url: 'https://docs.nestjs.com' };
}
}
@Get(':aa/param/:bb')
routeParameter(
@Param() params: { [key: string]: string },
@Param('aa') aa: string,
@Param('bb') bb: string,
) {
return params.aa + bb;
}
}
import { Controller, Get, HostParam } from '@nestjs/common';
@Controller({ host: ':name.api.localhost' })
export class ApiController {
@Get('he*lo/*')
getHello(@HostParam('name') name: string): string {
return name;
}
}
import { Controller, Post, Body, Query, Get } from '@nestjs/common';
import { CreateBoardDto } from './dto/create-board.dto';
import { GetBoardsDto } from './dto/get-board.dto';
@Controller('board')
export class BoardController {
@Post()
create(@Body() createBoardDto: CreateBoardDto) {
const { title, content } = createBoardDto;
return title + content;
}
@Get()
get(@Query() getBoardsDto: GetBoardsDto) {
const { offset, limit } = getBoardsDto;
console.log(offset, limit);
}
}