Skip to content

Instantly share code, notes, and snippets.

@peterkimzz
Created April 25, 2019 01:17
Show Gist options
  • Select an option

  • Save peterkimzz/93888e883944717e5f9d97ccf78a7f18 to your computer and use it in GitHub Desktop.

Select an option

Save peterkimzz/93888e883944717e5f9d97ccf78a7f18 to your computer and use it in GitHub Desktop.
nodejs cors 설정
const cors = (req, res, next) => {
// 와일드카드 (*)가 안먹혀서 일일이 배열안에 적어주면 됨
const allowedOrigins = [
'http://localhost:3000',
'http://localhost:3001',
'http://localhost:3002',
'http://localhost:3003',
'https://test.domain.com',
'https://domain.com',
'https://www.domain.com',
'https://admin.domain.com'
]
const origin = req.headers.origin
if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin)
res.setHeader('Access-Control-Allow-Credentials', true)
}
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
res.header(
'Access-Control-Allow-Headers',
'Content-Type,X-Requested-With,x-access-token'
)
res.header('Access-Control-Allow-Credentials', true)
next()
}
module.exports = cors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment