1// Add headers
2app.use(function (req, res, next) {
3
4 // Website you wish to allow to connect
5 res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8888');
6
7 // Request methods you wish to allow
8 res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
9
10 // Request headers you wish to allow
11 res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
12
13 // Set to true if you need the website to include cookies in the requests sent
14 // to the API (e.g. in case you use sessions)
15 res.setHeader('Access-Control-Allow-Credentials', true);
16
17 // Pass to next layer of middleware
18 next();
19});
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});
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})