2

I've spent a couple hours trying to figure this out...

app.use(function(req, res, next) { 
  res.header('Access-Control-Allow-Origin', "*"); 
  res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE'); 
  res.header('Access-Control-Allow-Headers', 'Content-Type'); 
  next();
});

app.all('*', function(req, res, next) {
   res.header("Access-Control-Allow-Origin", "*");
   res.header("Access-Control-Allow-Headers", "X-Requested-With");
   next();
});

Didn't work for me... I have this in my servers app.js right after the requires at the top.

Also tried the npm install cors and app.use(cors()). Didn't work either.

Why would I be experiencing this issue? I am using the express-generator (npm install express-generator)

2
  • seems like You're using some webserver (apache, nginx) infront of Your app, somethings are stripping that headers. I'm using cors and it works well. Remember to add custom headers before route handlers, cuz express routing is set of middlewares and the order of them may be the source of Your issue.
    – num8er
    Commented Dec 5, 2016 at 6:43
  • try to put: app.use(cors()) right after app = express()
    – num8er
    Commented Dec 5, 2016 at 6:45

1 Answer 1

7
req.open("GET", "localhost:3001/quote");

Should have been...

req.open("GET", "http://localhost:3001/quote");

The headers weren't the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.