Skip to content

Commit

Permalink
Add CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
brendannee committed Jul 21, 2020
1 parent 5718525 commit f75bb70
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -27,6 +27,7 @@
"auto-link": "^1.0.0",
"babel-runtime": "^6.26.0",
"classnames": "^2.2.6",
"cors": "^2.8.5",
"isomorphic-unfetch": "^3.0.0",
"lodash": "^4.17.19",
"next": "^9.4.5-canary.42",
Expand Down
23 changes: 23 additions & 0 deletions pages/api/route.js
@@ -1,3 +1,23 @@
import Cors from 'cors'

const cors = Cors({
methods: ['GET', 'HEAD'],
})

// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req, res, fn) {
return new Promise((resolve, reject) => {
fn(req, res, (result) => {
if (result instanceof Error) {
return reject(result)
}

return resolve(result)
})
})
}

function getServerUrl(scenario) {
if (scenario === '1' || scenario === '2' || scenario === '3') {
return 'http://ec2-54-208-197-111.compute-1.amazonaws.com'
Expand All @@ -9,6 +29,9 @@ function getServerUrl(scenario) {
}

export default async (request, response) => {
// Run the CORS middleware
await runMiddleware(request, response, cors)

const { lat1, lng1, lat2, lng2, scenario } = request.query

const server = getServerUrl(scenario)
Expand Down
15 changes: 14 additions & 1 deletion yarn.lock
Expand Up @@ -3556,6 +3556,14 @@ core-util-is@~1.0.0:
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=

cors@^2.8.5:
version "2.8.5"
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
dependencies:
object-assign "^4"
vary "^1"

cosmiconfig@^5.0.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.2.1.tgz#040f726809c591e77a17c0a3626ca45b4f168b1a"
Expand Down Expand Up @@ -6613,7 +6621,7 @@ obj-props@^1.0.0:
resolved "https://registry.yarnpkg.com/obj-props/-/obj-props-1.3.0.tgz#8884ab21c8d8496c4a7f696c78bf82289c51680b"
integrity sha512-k2Xkjx5wn6eC3537SWAXHzB6lkI81kS+icMKMkh4nG3w7shWG6MaWOBrNvhWVOszrtL5uxdfymQQfPUxwY+2eg==

object-assign@*, object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
object-assign@*, object-assign@^4, object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
Expand Down Expand Up @@ -9111,6 +9119,11 @@ validate-npm-package-license@^3.0.1:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"

vary@^1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=

vendors@^1.0.0:
version "1.0.4"
resolved "https://registry.yarnpkg.com/vendors/-/vendors-1.0.4.tgz#e2b800a53e7a29b93506c3cf41100d16c4c4ad8e"
Expand Down

1 comment on commit f75bb70

@vercel
Copy link

@vercel vercel bot commented on f75bb70 Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.