Last active
January 21, 2020 02:20
-
-
Save HaiBV/21407dddfcfc961a0c279ed8680b8565 to your computer and use it in GitHub Desktop.
Resolved CORS issue
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
// 1. Install extend enable CORS in web browser (simple, fast but situation) | |
// 2. Using middleware plug-in in server | |
// https://expressjs.com/en/resources/middleware/cors.html | |
var express = require('express') | |
var cors = require('cors') | |
var app = express() | |
app.use(cors()) | |
// 3. Using custom middleware in server | |
// https://enable-cors.org/server_expressjs.html | |
app.use(function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "YOUR-DOMAIN.TLD"); // update to match the domain you will make the request from | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
app.get('/', function(req, res, next) { | |
// Handle the get for this route | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment