Skip to content

Instantly share code, notes, and snippets.

@FlameWolf
Created July 18, 2022 15:47
Show Gist options
  • Save FlameWolf/cbe5187b72cde1c85865a2bfe53d731e to your computer and use it in GitHub Desktop.
Save FlameWolf/cbe5187b72cde1c85865a2bfe53d731e to your computer and use it in GitHub Desktop.
Configure CORS in Express using middleware
const app = express();
app.use(async (req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", process.env.ALLOW_ORIGIN);
res.setHeader("Access-Control-Allow-Credentials", true);
res.setHeader("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept, X-Slug, X-UID");
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, POST, PUT, PATCH, GET, DELETE");
next();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment