what is cors middleware code example

Example 1: express get request origin

app.use((req, res, next) => {
  const host = req.get('host');
  const origin = req.get('origin');
});

Example 2: cors package install npm

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')})

Tags:

Misc Example