Allow CORS REST request to a Express/Node.js application on Heroku Allow CORS REST request to a Express/Node.js application on Heroku express express

Allow CORS REST request to a Express/Node.js application on Heroku


I've cheked your code on a clean ExpressJS app and it works just fine.

Try move your app.use(allowCrossDomain) to the top of configure function.


I'm adding this as an answer only because the original post was put in as a comment and as such it got overlooked by yours truly the first time I went over this page.

As @ConnorLeech points out in his comment to the accepted answer above, there is a very handy npm package called, not surprisingly, cors. It's use is as simple as var cors = require('cors'); app.use(cors()); (again, cribbed from Mr. Leech's answer) and can also be applied in a stricter, more configurable fashion as outlined in their docs.

It may also be worth pointing out that the original comment I refer to above was made in 2014. It's 2019 now and looking at the npm package's github page the repo was updated as recently as nine days ago.


for supporting cookies withCredentials you need this line xhr.withCredentials = true;

mdn docs xhr.withCredentials

In the Express Server add this block before all the other

`app.all('*', function(req, res, next) {     var origin = req.get('origin');      res.header('Access-Control-Allow-Origin', origin);     res.header("Access-Control-Allow-Headers", "X-Requested-With");     res.header('Access-Control-Allow-Headers', 'Content-Type');     next();});`