Skip to content

Instantly share code, notes, and snippets.

@claytonbez
Created October 15, 2018 06:44
Show Gist options
  • Save claytonbez/1750a148d981ae90e887db0528154d31 to your computer and use it in GitHub Desktop.
Save claytonbez/1750a148d981ae90e887db0528154d31 to your computer and use it in GitHub Desktop.
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