import express from 'express'
var app = express();
app.all('*', (req, res, next) => {
console.log(req.headers)
const {
origin,
Origin,
referer,
Referer
} = req.headers
const allowOrigin = origin || Origin || referer || Referer || '*'
res.header('Access-Control-Allow-Origin', allowOrigin)
res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization,x-Requested-with')
res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
res.header('Access-Control-Allow-Credentials', true) // 可以带cookies
res.header('X-Powered-By', 'Express')
if (req.method === 'OPTIONS') { // OPTIONS是预请求在发送复杂请求时会先发送这个请求用来确认安全
res.sendStatus(200) // 当OPTIONS之后返回200时才会发送真正的请求
// 当发送的请求中携带cookie时就是一个复杂的请求
} else {
next()
}
})
vue-resource
this.$http.post(url,{},{emulateJSON:true,credentials:true})
// 设置credentials:true是允许接收cookie的凭证