Last active
November 21, 2018 06:14
-
-
Save ritwickdey/36682dabe4a992c57e4562c935bfbbdd to your computer and use it in GitHub Desktop.
CORS Setup for Node.js
This file contains hidden or 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 express = require('express'); | |
const app = express(); | |
//CORS Setup | |
app.use((req, res, next) => { | |
res.header('Access-Control-Allow-Origin', '*'); // Allowed Origins. | |
res.header('Access-Control-Allow-Headers', '*'); // Allowed Headers. | |
res.header('Access-Control-Expose-Headers', 'token'); // Exposed Headers - means client only can access those headers. | |
if (req.method === 'OPTIONS') { | |
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE'); // Allowed Methods. | |
res.header('Access-Control-Max-Age', 5 * 24 * 60 * 60); //5 days... But Chrome will take its MAX value. :D | |
return res.status(200).json({}); | |
} | |
next(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment