christianalfoni / webpack-express-boilerplate

A boilerplate for running a Webpack workflow in Node express
MIT License
1.4k stars 292 forks source link

CORS #43

Open tadwhitaker opened 8 years ago

tadwhitaker commented 8 years ago

I'm trying to make a call to the Yelp API, but keep getting the following error:

XMLHttpRequest cannot load https://api.yelp.com/v2/search?term=food&location=new%20york. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 400.

Any suggestions on how to work around this?

armandomendivil commented 8 years ago

You need something like this: http://enable-cors.org/server_expressjs.html http://stackoverflow.com/questions/18310394/no-access-control-allow-origin-node-apache-port-issue

christianalfoni commented 8 years ago

I think you rather want to proxy that API call through express @tadwhitaker . It is Yelp not having the necessary headers to you can not do much about that. But Node can be a proxy handling the request.

So:

var request = require('request');
app.get('/yelp', function (req, res) {
  request('/api.yelp.com....', function (error, response) {
    res.send(response.body); // Check request API, do not remember this
  })
});
Odywan55 commented 8 years ago

Can any-body advice me the way of handling server changes, with no rerun the server manualy, and working with routes on it, thanks a lot !!