express options request 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: express js cors
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/user/:id', function (req, res, next) {
res.json({user: 'CORS enabled'})
})
app.listen(5000, function () {
console.log('CORS-enabled web server listening on port 5000')
})
Example 3: express getting options instead of post
var express = require('express')
, cors = require('cors')
, app = express();
const corsOptions = {
origin: true,
credentials: true
}
app.options('*', cors(corsOptions));
app.listen(80, function(){
console.log('CORS-enabled web server listening on port 80');
});