ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ NestJS Interceptor ์์ ๊ฐ์ด๋ – ์๋ต ๋ณํ, ๋ก๊น , ์บ์ฑ, ์ฑ๋ฅ ์ถ์ ๊น์ง
octo54 2025. 4. 23. 14:26๐ NestJS Interceptor ์์ ๊ฐ์ด๋ – ์๋ต ๋ณํ, ๋ก๊น , ์บ์ฑ, ์ฑ๋ฅ ์ถ์ ๊น์ง
NestJS์์ **Interceptor(์ธํฐ์
ํฐ)**๋ Controller ๋ฉ์๋ ์ ํ๋ก ์คํ๋์ด, ์๋ต์ ๊ฐ๊ณตํ๊ฑฐ๋ ์์ฒญ์ ๊ฐ์ธ๋ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
์ด ๊ธ์ NestJS ๊ณต์ ๋ฌธ์ Interceptors๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๊ธ ๋ฒ์ญ + ์ค์ ์ค์ฌ ํด์ค๋ก ์ ๋ฆฌํ์ต๋๋ค.
๐ก Interceptor๋ NestJS์ "Aspect-Oriented Programming(AOP)"์ ์คํํ๋ ๊ฐ์ฅ ๋ํ์ ์ธ ๋๊ตฌ์ ๋๋ค.
โ Interceptor๋ ์ธ์ ์ฌ์ฉํ๋์?
- ์๋ต ๋ฐ์ดํฐ ๊ตฌ์กฐ ๋ณ๊ฒฝ
- ์์ฒญ/์๋ต ๋ก๊น
- ์์ฒญ ์๊ฐ ์ธก์ (Performance)
- ๊ฒฐ๊ณผ ์บ์ฑ
- ์๋ฌ ๋ฆฌํฌํธ ๊ฐ์ธ๊ธฐ (try/catch ๋์ )
1๏ธโฃ Interceptor ๊ธฐ๋ณธ ๊ตฌ์กฐ
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
@Injectable()
export class LoggingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const now = Date.now();
return next.handle().pipe(
tap(() => console.log(`โฑ ์ฒ๋ฆฌ ์๊ฐ: ${Date.now() - now}ms`)),
);
}
}
๊ตฌ์กฐ ์ค๋ช
- intercept()๋ ์์ฒญ์ ๊ฐ๋ก์ฑ๋ ๋ฉ์๋
- next.handle()์ ์ค์ ํธ๋ค๋ฌ ์คํ
- tap()์ ์๋ต์ด ์๋ฃ๋ ํ ํ์ฒ๋ฆฌ
2๏ธโฃ ๋ผ์ฐํธ์ Interceptor ์ ์ฉํ๊ธฐ
@UseInterceptors(LoggingInterceptor)
@Get()
getData() {
return { message: 'Hello Interceptor!' };
}
@UseInterceptors() ๋ฐ์ฝ๋ ์ดํฐ๋ฅผ ํตํด ํน์ ์ปจํธ๋กค๋ฌ/๋ผ์ฐํธ์ ์ธํฐ์ ํฐ ์ ์ฉ ๊ฐ๋ฅํฉ๋๋ค.
์ด ์ง์ ์ ์ค๋ฌด ์๋ต ๊ฐ๊ณต ๋ฐ ๋ก๊น ์์ ๊ฐ ๋์ค๋ ๊ตฌ๊ฐ์ผ๋ก ์ ๋์ผ์ค ๊ด๊ณ ์ฝ์ ์ต์ ์์น์ ๋๋ค.
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXXX"
data-ad-slot="YYYYYYY"
data-ad-format="auto"
data-full-width-responsive="true"></ins>
3๏ธโฃ ์๋ต ๋ฐ์ดํฐ ๊ฐ๊ณต Interceptor ์์
@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<T, ApiResponse<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<ApiResponse<T>> {
return next.handle().pipe(
map(data => ({
success: true,
data,
timestamp: new Date().toISOString(),
})),
);
}
}
interface ApiResponse<T> {
success: boolean;
data: T;
timestamp: string;
}
์ปจํธ๋กค๋ฌ์์ returnํ ์์ ๋ฐ์ดํฐ๊ฐ, ๊ฐ๊ณต๋ JSON ํํ๋ก ์๋ ๋ณ๊ฒฝ๋ฉ๋๋ค.
4๏ธโฃ ์ ์ญ Interceptor ๋ฑ๋ก
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalInterceptors(new TransformInterceptor());
await app.listen(3000);
}
์ ์ญ ๋ฑ๋ก์ ํตํด ๋ชจ๋ ๋ผ์ฐํธ์์ ๊ณตํต๋ ์๋ต ๊ตฌ์กฐ ์ ์ง๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
5๏ธโฃ ์บ์ฑ ์ธํฐ์ ํฐ (์ค๋ฌด ์์)
@Injectable()
export class CacheInterceptor implements NestInterceptor {
private cache = new Map<string, any>();
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const key = context.switchToHttp().getRequest().url;
if (this.cache.has(key)) {
return of(this.cache.get(key));
}
return next.handle().pipe(
tap(data => this.cache.set(key, data)),
);
}
}
์ค๋ฌด์์ Redis์ ์ฐ๋ํ์ฌ ๊ณ ๋ํ๋ ์บ์ ์์คํ ๋ ๊ตฌํ ๊ฐ๋ฅํฉ๋๋ค.
6๏ธโฃ Error ๊ฐ์ธ๊ธฐ (์คํ ๊ฒฐ๊ณผ Wrapper)
@Injectable()
export class ErrorWrapperInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
catchError(err => {
return throwError(() => new HttpException({
success: false,
error: err.message || '์ ์ ์๋ ์ค๋ฅ',
}, err.status || 500));
}),
);
}
}
๐ง Interceptor vs Middleware vs Guard vs Pipe
ํญ๋ชฉ ์์ ์ญํ
Middleware | Controller ์ง์ ์ | ์์ฒญ ๋ก๊น , ์ฟ ํค ํ์ฑ ๋ฑ |
Guard | ๋ผ์ฐํธ ์ง์ ์ | ์ธ์ฆ/์ธ๊ฐ |
Pipe | ํธ๋ค๋ฌ ์คํ ์ | ์ ํจ์ฑ ๊ฒ์ฌ, ํ์ ๋ณํ |
Interceptor | ์คํ ์ /ํ | ์๋ต ๊ฐ๊ณต, ๋ก๊น , ์บ์ฑ |
๐ ๋ง๋ฌด๋ฆฌ ์์ฝ
- Interceptor๋ ์๋ต ์ฒ๋ฆฌ, ๋ก๊น , ๋ณํ, ์บ์ฑ ๋ฑ ํ์ฒ๋ฆฌ์ ์ต์ ํ๋ ๋๊ตฌ
- CallHandler.handle()์ Observable ๊ธฐ๋ฐ → RxJS ์ฐ์ฐ์ ์ฌ์ฉ ๊ฐ๋ฅ
- ์ค๋ฌด์์ TransformInterceptor, ErrorInterceptor, CacheInterceptor๋ ํ์ ๋์ ์์
NestJS Interceptor,NestJS ์๋ต ๊ฐ๊ณต,NestJS ์๋ต ํฌ๋งท,NestJS ๋ก๊น Interceptor,NestJS TransformInterceptor,NestJS ์ ์ญ ์ธํฐ์ ํฐ,NestJS ์บ์ ์ธํฐ์ ํฐ,NestJS Error ์ฒ๋ฆฌ,NestJS ์ฑ๋ฅ์ธก์ ,NestJS ๊ตฌ์กฐํ ์๋ต
'framework > NestJS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- Total
- Today
- Yesterday
- seo ์ต์ ํ 10๊ฐ
- ๋ฐฑ์๋๊ฐ๋ฐ
- PostgreSQL
- AI ์๋ํ
- Ktor
- ์น๊ฐ๋ฐ
- REACT
- fastapi
- Prisma
- nextJS
- ํ๋ก ํธ์๋
- ์ค๋งํธ ์ปจํธ๋ํธ
- Next.js
- LangChain
- NestJS
- nodejs
- SEO์ต์ ํ
- github
- ๋ฐฑ์๋
- rag
- CI/CD
- gatsbyjs
- Webpack
- kotlin
- Docker
- AI์ฑ๋ด
- llm
- ๊ด๋ฆฌ์
- App Router
- ๊ฐ๋ฐ๋ธ๋ก๊ทธ
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |