1var express = require('express')
2var cors = require('cors')
3var app = express()
4
5app.use(cors())
6
7app.get('/products/:id', function (req, res, next) {
8 res.json({msg: 'This is CORS-enabled for all origins!'})
9})
10
11app.listen(80, function () {
12 console.log('CORS-enabled web server listening on port 80')
13})
14
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// CORS (Cross-Origin Resource Sharing) headers to support Cross-site HTTP requests
2
3app.all('*', function(req, res, next) {
4 res.header("Access-Control-Allow-Origin", "*");
5 res.header("Access-Control-Allow-Headers", "X-Requested-With");
6 res.header('Access-Control-Allow-Headers', 'Content-Type');
7 next();
8});
9
1app.use(function(req, res, next) {
2 res.header("Access-Control-Allow-Origin", "YOUR-DOMAIN.TLD"); // update to match the domain you will make the request from
3 res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
4 next();
5});
1//install
2npm install cors
3//use
4var express = require('express')
5var cors = require('cors')
6var app = express()
7
8app.use(cors())
9
10app.get('/products/:id', function (req, res, next) {
11 res.json({msg: 'This is CORS-enabled for all origins!'})
12})
13
14app.listen(80, function () {
15 console.log('CORS-enabled web server listening on port 80')
16})
17
1//Cors = Cross-origin resource sharing
2
3var express = require('express');
4var cors = require('cors');
5var app = express();
6
7app.use(cors());
8
9var corsOptions = {
10 origin: 'http://example.com',
11 optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
12}
13
14app.listen(80, function () {
15 console.log('CORS-enabled web server listening on port 80')
16})