NodeJS WebAPI (Express JS) CORS

Date: 2016-10-28

Source: https://williambert.online/2013/06/allow-cors-with-localhost-in-chrome/


app.all("/api/*", function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
  res.header("Access-Control-Allow-Methods", "GET, PUT, POST");
  return next();
});

app.all("/api/*", function(req, res, next) {
  if (req.method.toLowerCase() !== "options") {
    return next();
  }
  return res.send(204);
});

5420cookie-checkNodeJS WebAPI (Express JS) CORS