Created
October 15, 2018 06:44
-
-
Save claytonbez/1750a148d981ae90e887db0528154d31 to your computer and use it in GitHub Desktop.
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 express = require('express'); | |
const app = express(); | |
//acts as a middleware | |
//to handle CORS Errors | |
app.use((req, res, next) => { //doesn't send response just adjusts it | |
res.header("Access-Control-Allow-Origin", "*") //* to give access to any origin | |
res.header( | |
"Access-Control-Allow-Headers", | |
"Origin, X-Requested-With, Content-Type, Accept, Authorization" //to give access to all the headers provided | |
); | |
if(req.method === 'OPTIONS'){ | |
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET'); //to give access to all the methods provided | |
return res.status(200).json({}); | |
} | |
next(); //so that other routes can take over | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment