cors npm use code example

Example 1: node express cors headers

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

Example 2: cors npm

/* 
Installation
$ npm install cors
*/

// Simple Usage (Enable All CORS Requests)
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");
});

Example 3: cors npm

installation :

$ npm i cors

usage :

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

Example 4: 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')})