동일 출처 정책(same-origin policy)은 어떤 출처에서 불러온 문서나 스크립트가 다른 출처에서 가져온 리소스와 상호작용하는 것을 제한하는 중요한 보안 방식입니다. 동일 출처 정책은 잠재적으로 해로울 수 있는 문서를 분리함으로써 가능한 공격 경로를 줄이는데 도움을 줍니다.

app.js
const cors = require('cors'); //middleware require const corsOptions = { //cors option value origin: 'http://localhost:3000', //must write host url credentials: true //preflight auth }; app.use(cors(corsOptions)); //middleware used app.use(function(err, req, res, next) { ... //Response CORS res.header('Access-Control-Allow-Origin', 'http://localhost:3000'); res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); res.header('Access-Control-Allow-Credentials', true); ... });
fetch.js
fetch('url', { headers: { 'Content-Type' : 'application/json', }, credentials: 'include', //자격증명 옵션 method: 'POST', mode: 'cors', body: JSON.stringify(data), }) .then((res) => { //response edit }) .then((rs) => { //result data edit });