Cors With Additional Options

[Solved] Cors With Additional Options | Php - Code Explorer | yomemimo.com
Question : cors with additional options

Answered by : ashutosh-panigrahy

{"tags":[{"tag":"textarea","content":"var express = require('express')\nvar cors = require('cors')\nvar app = express()\n\nvar allowlist = ['http:\/\/example1.com', 'http:\/\/example2.com']\nvar corsOptionsDelegate = function (req, callback) {\n var corsOptions;\n if (allowlist.indexOf(req.header('Origin')) !== -1) {\n corsOptions = { origin: true } \/\/ reflect (enable) the requested origin in the CORS response\n } else {\n corsOptions = { origin: false } \/\/ disable CORS for this request\n }\n callback(null, corsOptions) \/\/ callback expects two parameters: error and options\n}\n\napp.get('\/products\/:id', cors(corsOptionsDelegate), function (req, res, next) {\n res.json({msg: 'This is CORS-enabled for an allowed domain.'})\n})\n\napp.listen(80, function () {\n console.log('CORS-enabled web server listening on port 80')\n})","code_language":"whatever"}]}

Source : https://expressjs.com/en/resources/middleware/cors.html | Last Update : Tue, 02 Jan 24

Answers related to cors with additional options

Code Explorer Popular Question For Php