我正在尝试通过Cookie和Authorization标头发送令牌以确保来自标头的用户名和来自已解码令牌的用户名相同。但问题是,当我发送授权头时,在后端OPTIONS请求失败,出现204错误。没有标题,它完美地运作。 我尝试过的: 添加了用于处理此问题的中间件,OPTIONS仍然是204。
app.use((req, res, next) => {
console.log(`headers`)
console.log(req.headers)
res.header("Access-Control-Allow-Credentials", "true")
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "X-Requested-With")
res.header("Access-Control-Allow-Headers", "Content-Type")
res.header("Access-Control-Allow-Headers", "Authorization")
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS")
if ('OPTIONS' == req.method) {
res.send(200)
}
else {
next();
}
})
app.use(cors({withCredentials: true, credentials: true, origins: 'http://localhost:8080'}))
设置socket.io
var socket = require('socket.io')
var app = express(),
var http = require('http').createServer(app)
const io = new socket(http, { path: '/api/chat', origins: 'http://localhost:8080' })
http.listen(3000, () => console.log(`App is running on localhost:3000`.bold.yellow))
const events = require('./src/socket')(io);
但它仍然无效。我可能错过了什么?