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.

Revisions

  1. peterkimzz created this gist Apr 25, 2019.
    29 changes: 29 additions & 0 deletions cors.middleware.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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