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
- class
- Nest.js
- jest
- MongoDB
- GIT
- Queue
- nodejs
- 정렬
- mongoose
- Sequelize
- react
- JavaScript
- nestjs
- 게임
- typeORM
- Dinosaur
- Python
- OCR
- Bull
- TypeScript
- Express
- MySQL
- flask
- 자료구조
- game
- 공룡게임
- AWS
- cookie
- dfs
Archives
- Today
- Total
포시코딩
2월15일 - Nest.js에서의 CORS 본문
728x90
개요
express에서 cors를 사용하려면 cors 라이브러리를 설치해 직접 설정까지 해줬어야 했는데
Nest.js에는 기본으로 있지 않을까 싶어 찾아봤더니 역시나 그냥 갖다 쓰면 되게끔 구현되어 있었다.
https://docs.nestjs.com/security/cors
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reac
docs.nestjs.com
사용방법
app.enableCors();
이 한줄 추가해주면 끝이다.
src/main.ts
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors();
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
);
await app.listen(8080);
}
혹시나 싶어 class-transformer 를 사용하는 app.useGlobalPipe() 앞 부분에서 적용되게 위치시켰다.
적용 시 문제 없이 다른 클라이언트 서버에서 Nest.js 백엔드 서버로 잘 접근하는걸 확인할 수 있다.
세부 설정을 추가할 수도 있는데 일단은 그냥 이렇게 쓰고
만약 추가할 일이 생긴다면 더 알아봐야겠다.
728x90
'TIL' 카테고리의 다른 글
2월18일 - Naver Maps API (1) | 2023.02.19 |
---|---|
2월16일 - 문서의 로드 시점을 다루는 코드 비교, lodash (0) | 2023.02.16 |
2월14일 - JavaScript 전개구문을 통해 Object 합치기 (0) | 2023.02.14 |
2월13일 - 내가 구현한 로직 설명하는 연습하기 - 작성중 (0) | 2023.02.14 |
2월12일 - JavaScript navigator.mediaDevices를 통한 카메라 제어 (0) | 2023.02.12 |