Skip to content

Instantly share code, notes, and snippets.

@HaiBV
Last active January 21, 2020 02:20
Show Gist options
  • Save HaiBV/21407dddfcfc961a0c279ed8680b8565 to your computer and use it in GitHub Desktop.
Save HaiBV/21407dddfcfc961a0c279ed8680b8565 to your computer and use it in GitHub Desktop.
Resolved CORS issue
// 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