1
2
3
4
5
6
/**
http://localhost:8000 => http://localhost:8000 = same origin
http://localhost:8000 => http://localhost:2000 = cross origin
http://localhost:8000 => http://weather.com = cross origin
**/
1
2
3
4
app.use((req,res, next) => {
res.header("Access-Control-Allow-Origin", "*"); // 모든 도메인
res.header("Access-Control-Allow-Origin", "https://foo.example"); // 특정 도메인
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var express = require('express')
var cors = require('cors')
var app = express()
// 모든 도메인 접근 허용
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
// 특정 도메인 접근 허용
var corsOptions = {
origin: 'https://foo.example',
optionsSuccessStatus: 200
}
app.get('/products/:id', cors(corsOptions), function (req, res, next) {
res.json({msg: 'This is CORS-enabled for only example.com.'})
})
1
// 'https://cors-anywhere.herokuapp.com/https://foo.example/'
1
2
3
4
5
6
7
8
// webpack.config.js
module.exports = {
devServer: {
proxy: {
'/api': 'http://localhost:8000'
}
}
};
https://developer.mozilla.org/ko/docs/Web/Security/Same-origin_policy
https://developer.mozilla.org/ko/docs/Web/HTTP/CORS
https://ko.wikipedia.org/wiki/%ED%94%84%EB%A1%9D%EC%8B%9C_%EC%84%9C%EB%B2%84
https://velog.io/@yejinh/CORS-4tk536f0db