Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 캐시메모리DB저장
- Nest
- 로그파일저장
- NEST좌석
- javascript
- cachememory
- 좌석생성
- Class Validation
- devcamp
- 좌석만들기
- CRUD
- Nest.js
- ormconfig.ts
- 캐시메모리저장후DB저장
- Til
- get
- Live server
- 카카오페이테스트결제
- 내일배움캠프
- 네스트로거
- 좌석내가격
- 포트원
- 자바스크립트
- TYPESCRIPT좌석생성
- joi vs classvalidation
- nestwinston
- 테스트결제
- TypeORMconfig.ts
- NEST좌석생성
- TypeORMconfig
Archives
- Today
- Total
배씨의 개발일지
TS-미들웨어 전역 사용하기 본문
Auth관련 미들웨어를 처리 중
타입 스크립트의 app.module에서 일일이 URL을 입력해주다 보니
나중에 이르러서는 너무나도 많은 URL을 작성해줘야 했다.
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(AuthMiddleware)
.forRoutes(
{ path: 'user/update', method: RequestMethod.PUT },
{ path: 'board', method: RequestMethod.POST },
{ path: 'board', method: RequestMethod.GET },
{ path: 'board/:board_id', method: RequestMethod.GET },
{ path: 'board/waiting', method: RequestMethod.GET },
{ path: 'board/waiting/:board_id', method: RequestMethod.DELETE },
{ path: 'board/member/:board_id', method: RequestMethod.POST },
{ path: 'board/waiting/:board_id', method: RequestMethod.POST },
{ path: 'comment/:card_id', method: RequestMethod.POST },
{ path: 'comment/:comment_id', method: RequestMethod.PUT },
{ path: 'comment/:comment_id', method: RequestMethod.DELETE },
);
}
}
이런 식으로 URL이 추가될 때마다 하나하나 일일이 작성해 줘야 했는데
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(AuthMiddleware)
.exclude(
// 예외 url 지정
{ path: 'user/sign', method: RequestMethod.POST },
{ path: 'user/login', method: RequestMethod.POST }
)
.forRoutes('*'); // middleware를 모든 경로에 적용
}
}
이러한 방식을 통해서 모든 URL에서 미들웨어를 사용할 수 있게 해 주고
그 외 사용하지 않을 URL만 등록해 주는 방식으로 변경했다.
exclude = 예외로 지정할 url을 등록하기
.forRoutes('*')를 통해 middleware를 모든 경로에 적용한다
추가적으로 변경해줘야 하는 부분은
원래는 main.ts에서 따로 미들웨어에 대한 설정을 안 해줘도 됐었지만
미들웨어를 전역에서 사용하기 위해서는
app.use(authMiddleware.use.bind(authMiddleware));
이런 식으로 따로 미들웨어를 사용한다고 지정해주어야 했다.
왜 인지는 모르겠지만 authMiddleware에서 몇 개의 인수값이 필요한데 넘어오지 않는다고 에러가 떠서
따로 bind를 걸어주어 그 에러를 해결하였다.
'TIL' 카테고리의 다른 글
NEST.JS JWT와 GUARD 사용하기 (0) | 2023.08.21 |
---|---|
Primary key, Foreign Key (0) | 2023.08.21 |
MVC 패턴 (0) | 2023.08.17 |
3 tier architecture (0) | 2023.07.24 |
VScode로 mongoDB연결 (0) | 2023.06.13 |
Comments