Skip to content

Instantly share code, notes, and snippets.

@matthewoestreich
Created July 13, 2019 17:42
Show Gist options
  • Save matthewoestreich/26cdb8c357e5be3a292cad0806d85ad3 to your computer and use it in GitHub Desktop.
Save matthewoestreich/26cdb8c357e5be3a292cad0806d85ad3 to your computer and use it in GitHub Desktop.
// CORS middleware function (this will allow any host to be able to send requests)
function allowCors(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
}
// This is how you tie it into your app -
// NOTE: This opens up every single route - this is merely an example
const express = require('express');
const app = express();
// other stuff here
// add CORS middleware
///// SET UP CORS //////
app.use(allowCors); ////
////////////////////////
// more stuff here
let listenOn = 5000;
app.listen(listenOn, () => {
console.log(`App started on port ${listenOn}`)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment