Last active
September 26, 2020 06:49
-
-
Save heytulsiprasad/6df4f71258f8060aa097030767e928ef to your computer and use it in GitHub Desktop.
Example setup for cors without using cors package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const origin = | |
process.env.NODE_ENV === "production" | |
? process.env.FRONTEND_PROD_URL | |
: process.env.FRONTEND_LOCAL_URL; | |
app.use((req, res, next) => { | |
res.header("Access-Control-Allow-Origin", origin); | |
res.header("Access-Control-Allow-Credentials", true); | |
if (req.method === "OPTIONS") { | |
res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET"); | |
return res.status(200).json({}); | |
} | |
next(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment