我试图用很多不同的方式设置cors,看看我是否可以解决这个问题。我无法获得适用于chrome的PUT / DELETE请求。但是他们适用于Firefox。我将粘贴下面的代码片段!
index.js
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser')
const cors = require('cors');
const ioHelper = require('./controllers/ioHelper')
const routes = require('./routes/routes');
require('dotenv').load();
const port = process.env.PORT || 3001;
const app = express();
app.use(cookieParser())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors())
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
const server = app.listen(port);
const io = require('socket.io')(server)
ioHelper(io)
routes(app);
mongoose.Promise = global.Promise;
mongoose.connect(process.env.DB_CONNECT, { useMongoClient: true });
module.exports = app;