amazon-archives / awsmobile-cli

CLI experience for Frontend developers in the JavaScript ecosystem.
Apache License 2.0
142 stars 35 forks source link

Access-Control-Allow-Origin header issue #127

Closed funkfinger closed 6 years ago

funkfinger commented 6 years ago

I am getting this error in a browser:

Failed to load https://XXXXXX.execute-api.us-east-1.amazonaws.com/Development/fitnessLevel: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://XXXXXX.cloudfront.net' is therefore not allowed access. The response had HTTP status code 502.`

after I alter the backend API file and awsmobile publish. I assume this may have something to do with a CORS hash? I can't find any documentation on how to update the backend and update the required CORS headers. Any help is much appreciated!

elorzafe commented 6 years ago

Your app.js should have this in order to work on the browser.

// declare a new express app
var app = express()
app.use(bodyParser.json())
app.use(awsServerlessExpressMiddleware.eventContext())

// Enable CORS for all methods
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
  next()
});

Have you try to call the api using the invoke command awsmobile cloud-api invoke ....

funkfinger commented 6 years ago

This looks like it's my screw-up - so sorry. I lint-ed the generated API output from the awsmobile command and ran into the above errors. when I returned the file to the generated output, the errors went away. I'm testing some more, but obviously, something got messed up on my end. Sorry for the inquirery and thanks so much for the help!