React-S3被CORS政策封锁

时间:2020-03-22 15:22:50

标签: reactjs express amazon-s3 cors

因此,我正在使用React-S3以便将文件从我的应用程序上传到S3存储桶。我正在使用代理服务器,但仍然出现以下错误

Access to fetch at 'https://atlantic-files.s3.amazonaws.com/' from origin 'http://localhost:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

这是我的服务器

const express = require('express');
const bodyParser = require('body-parser')
const path = require('path');
const app = express();

app.use(express.static(path.join(__dirname, 'build')));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', "*");
    res.header('Access-Control-Allow-Methods', "GET,PUT,POST,DELETE");
    res.header('Access-Control-Allow-Methods', "Content-Type");
    next();
});

app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname, 'build/index.html'), function (err) {
        if (err) {
            res.status(500).send(err)
        }
    })
})

app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(5000);


console.log('App is listening on port 5000');

有解决方案吗?

1 个答案:

答案 0 :(得分:0)

在要启用cors的路由上尝试cors。 https://expressjs.com/en/resources/middleware/cors.html

INT_MIN