Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cors support #891

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ NODE_ENV=

RPC_URL=
SERVER_PORT=
CORS_ORIGIN=
CORS_METHOD=
CONNECTION_STRING=
TRANSLATIONS_PATH=
PROCESS_EVENTS_DELAY=
Expand Down
27 changes: 13 additions & 14 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,28 @@ import { InviteRouter } from './Invite'
import { BidRouter, PublicationRouter } from './Listing'
import { AuthorizationRouter } from './Authorization'

env.load()

const SERVER_PORT = env.get('SERVER_PORT', 5000)
const CORS_ORIGIN = env.get('CORS_ORIGIN', '*')
const CORS_METHOD = env.get('CORS_METHOD', '*')

const app = express()
const httpServer = http.Server(app)

app.use(bodyParser.urlencoded({ extended: false, limit: '2mb' }))
app.use(bodyParser.json())
app.use((_, res, next) => {
res.setHeader('Access-Control-Allow-Origin', CORS_ORIGIN)
res.setHeader('Access-Control-Request-Method', CORS_METHOD)
res.setHeader(
'Access-Control-Allow-Methods',
'OPTIONS, GET, POST, PUT, DELETE'
)
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')

if (env.isDevelopment()) {
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Request-Method', '*')
res.setHeader(
'Access-Control-Allow-Methods',
'OPTIONS, GET, POST, PUT, DELETE'
)
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
next()
})

next()
})
} else {
if (!env.isDevelopment()) {
// This is not ideal, but adding newrelic to development is worst
require('newrelic')
}
Expand Down