Seltsam Seltsam - 9 months ago 57
AngularJS Question

Express app with Aurelua/Angular App Admin-Panel - localhost behaviour?

I'm having a Web-Project based on Express for serving the website and an Aurelia (or any Framework for that matter) App as a Admin-Panel to edit the contents of the Website.

I'm serving the Aurelia-App via the static/public directory of express. The Compiled Aurelia app is sitting inside the Express app. This works fine so far on my local machine.

I just now realized, that my CORS setup no longer works when I upload the Website to an external server, since the IP I'm sending requests to the Express-Api via the Aurelia-App is always the Machines IP where the Aurelia-App is loaded from - regardless of where the Aurelia-App is hosted, correct? Authorizing localhost for CORS has not affect anymore since the Aurelia-Admin App is executed from my local Machine?

What would be the correct approach to solve this?

Answer Source

Solution was actually fairly easy: just enable CORS for all requests in the express-app:

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", "Origin, X-Requested-With, Content-Type, Accept, un-populated");