npm install cors
var cors = require("cors");
CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options.
// Wide listing a cors to accept a specific domain route
const cors = require('cors');
const corsOption = {
origin: ['http://localhost:3000'],
};
app.use(cors(corsOption));
var express = require('express')var cors = require('cors')var app = express() app.get('/products/:id', cors(), function (req, res, next) { res.json({msg: 'This is CORS-enabled for a Single Route'})}) app.listen(80, function () { console.log('CORS-enabled web server listening on port 80')})
var express = require('express')var cors = require('cors')var app = express() app.use(cors()) app.get('/products/:id', function (req, res, next) { res.json({msg: 'This is CORS-enabled for all origins!'})}) app.listen(80, function () { console.log('CORS-enabled web server listening on port 80')})