软错误的严格的原产地时,跨简单node.js-reactJS项目

0

的问题

我试图像上传到Cloundinary,但发生了一个错误地位代码500有关软虽然我已经设置的服务器,以便允许所有来源。

错误消息是:

POST http://localhost:5000/my-route-upload 500 (Internal Server Error)

这里是我的职务功能:

const cloudinaryUpload = (fileToUpload) => {
    return axios.post(API_URL + '/cloudinary-upload', fileToUpload)
    .then(res => console.log(res))
    .catch(err => {
        console.log(err)
        console.log("cannot post")
    }); }

在服务器方面,我不得不添加下面的方框中App.JS

const cors = require('cors'); 
var app = express();
app.use(cors({
  origin: "*",
  })
);

这些代码有没有执行,我试着修改原产地为特定的一种喜欢 http://127.0.0.1:3001 (我的客户口3000). 然后就出来了另一个错误信息

回到第一个错误,在标签网络/Headers:

Request URL: http://localhost:5000/cloudinary-upload
Request Method: POST
Status Code: 500 
Referrer Policy: strict-origin-when-cross-origin

Access-Control-Allow-Origin: *

Host: localhost:5000
Origin: http://127.0.0.1:3000

我不知道为什么它不工作。 我用创造-反应-应对客户和快速生成的服务器

express node.js reactjs
2021-11-24 04:02:31
4
0

也许你应该添加的内容类型的头你的爱可信的请求。 这样的。

const res = await axios.post('url', data, {
  headers: {
    'content-type': 'application/json'
  }
});
2021-11-24 04:17:25

它仍然没有工作 const cloudinaryUpload = (fileToUpload) => { return axios.post(API_URL + '/cloudinary-upload', fileToUpload, {headers: { 'content-type': 'application/json' }}) .then(res => res.data) .catch(err => { console.log(err) console.log("cannot post") }); }
Ho Quang Lam

因为它是目前写的,你的回答是不清楚。 请 编辑 ,以增加额外的详细信息将帮助其他人理解如何解决此问题的要求。 你可以找到更多的信息如何编写很好的答案 在帮助中心.
Community
0

设置一个代理服务器从你的客户

代理可以是一个简单的 "proxy": "http://localhost:5000" 在你的包。式,其中所有未知的要求将被代理localhost:5000 基本上,你需要叫api从客户 /my-route-upload 而不是的 http://localhost:5000/my-route-upload.

但是首选的方法将是增添一个名为文件 src/setupProxy.js $ npm install http-proxy-middleware --save 添加这样的文件


module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:5000',
      changeOrigin: true,
    })
  );
};```

Also look at enabling cors in express
https://enable-cors.org/server_expressjs.html
2021-11-24 05:04:57
0
const cors = require('cors'); 
var app = express();
app.use(cors());

试试这个

2021-11-24 07:02:38

虽然这个代码可以回答这个问题,提供更多的背景下关于如何和/或为什么它解决的问题将会提高答复的长期价值。 你可以找到更多的信息如何编写很好的答案在帮助中心: stackoverflow.com/help/how-to-answer . 祝你好运
nima
0

这中间有助于避免交叉平台的错误

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

设置这头中间上你根文件上你的所有道路的表达应用程序,更新这个代码块服务器软框在AppJS

2021-11-24 09:08:05

我有固定的,它非常感谢你
Ho Quang Lam

与这个中间?
Smit Gajera

我有一个错误,在验证与Cloudanry. 但是错误似乎喜欢它来自软
Ho Quang Lam

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................