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
});
app.post('/', function(req, res, next) {
// Handle the post for this route
});
// Wide listing a cors to accept a specific domain route
const cors = require('cors');
const corsOption = {
origin: ['http://localhost:3000'],
};
app.use(cors(corsOption));
/*index.js*/
const express = require( 'express' );
const cors = require( 'cors' );
const app = express(); const port = 3030;
//cors is enabled through out the entire app
app.use( cors() );
app.get( '/users', (request, response, next) => { res.json( { info: 'cors is enabled' } ) });
app.listen( port, () => {
console.log(`App is running on ${port}`)
});
//install package => npm i cors
const cors = require('cors');
const app = express()
app.use(cors()); // use this cors in middleware and done
$ npm install cors