[WIL] What is CORS?
05/07/23
![[WIL] What is CORS?](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1683554475289%2F52e90069-93cd-47fa-b4b7-0798fe25e2c8.png&w=3840&q=75)
This week's reflection
Planning for the main project in my specialization, collaborating on both the front-end and back-end
API design and Jest unit testing
What is CORS?
CORS stands for Cross-Origin Resource Sharing, which refers to the sharing of resources across different origins.
By default, if a client sends a request to a server with a different domain, the request will be blocked. For example, if a client at localhost:8000 sends a request to a server at localhost:8080, the server will block the request because it came from a different port, i.e., a different domain. This will result in an error that says "Access-Control-Allow-Origin" header is missing.
To solve the CORS issue, we need to add the "Access-Control-Allow-Origin" header to the response header. This header indicates that we allow the request from the client domain.
// Allow all CORS
Access-Control-Allow-Origin: *
// Allow specific domain
Access-Control-Allow-Origin: http
CORS library in Node Express
In Node.js with Express, we can use the CORS library:
First, install the cors library
const cors = require('cors');To allow access from all domains
app.use(cors());To allow access only from specific domains:
const cors = require('cors'); let corsOptions = { origin: 'https://www.domain.com', credentials: true } app.use(cors(corsOptions));
![[코테] 그리디 문제 - 무지의 먹방 라이브](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1712215455263%2F1ac1f35a-8862-4e42-8d0c-e2bea01e04c0.png&w=3840&q=75)
![[코테] Bfs 토마토](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709032619170%2F70056896-c857-444b-9c99-45bfcb466806.png&w=3840&q=75)
![[코테] Dfs 문제 유형 - 그래프 내에서 구분하여 카운트 하기](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709019361383%2Fb0585d72-c808-4169-83a9-2724f312e927.png&w=3840&q=75)
![[코테] DFS vs BFS](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971211123%2F71f9386c-6a62-43b2-a602-4d084c24d6cf.png&w=3840&q=75)
![[코테] 여행경로](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971251412%2F27ce72ed-8ee7-4d13-a02f-ff4bbe50c4be.png&w=3840&q=75)