1app.use(function(req, res, next) {
2 res.header("Access-Control-Allow-Origin", "*");
3 res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
4 next();
5});
1app.use((req, res, next) => {
2 const host = req.get('host');
3 const origin = req.get('origin');
4});
1var express = require('express')
2var cors = require('cors') //use this
3var app = express()
4
5app.use(cors()) //and this
6
7app.get('/user/:id', function (req, res, next) {
8 res.json({user: 'CORS enabled'})
9})
10
11app.listen(5000, function () {
12 console.log('CORS-enabled web server listening on port 5000')
13})
1/*
2Installation
3$ npm install cors
4*/
5
6// Simple Usage (Enable All CORS Requests)
7var express = require("express");
8var cors = require("cors");
9var app = express();
10
11app.use(cors());
12
13app.get("/products/:id", function (req, res, next) {
14 res.json({ msg: "This is CORS-enabled for all origins!" });
15});
16
17app.listen(3000, function () {
18 console.log("CORS-enabled web server listening on port 3000");
19});
1installation :
2
3$ npm i cors
4
5usage :
6
7var express = require('express')
8var cors = require('cors')
9var app = express()
10
11app.use(cors())
12
13app.get('/products/:id', function (req, res, next) {
14 res.json({msg: 'This is CORS-enabled for all origins!'})
15})
16
17app.listen(80, function () {
18 console.log('CORS-enabled web server listening on port 80')
19})
20
1var 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')})