节点JavaScript Heroku-CORS成功地与所有API调用Exept上传图像API



我们的web应用程序有许多api,可以从部署在heroku上的节点服务器获取。除了其中一个允许用户将图像上传到节点服务器的api外,所有api都能成功工作。它在localhost中运行良好,但当我们在heroku上将其部署到生产中时,我们得到了has been blocked by CORS policy: 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.。我们尝试了一些关于stackoverflow的建议,但没有任何帮助。CORS错误只发生在这个特定的api中。所有其他api都可以正常工作。cors在处理图像时有其他方法吗?

***SERVER***
const express = require('express');
const bodyParser =  require('body-parser')
require('dotenv').config();
const https = require('https');
const port = process.env.PORT || 5000;
const app = express();
const cors = require('cors');
//Setting up the cors config
app.use(cors());

//Prevent application from crashing if incorrect route
app.all('*', (req, res, next) =>{
const err = new Error(`Requested URL ${req.path} not found!`);
err.statusCode = 404;
next(err);
})
app.use((err, req, res, next) =>{
const statusCode = err.statusCode || 500;
res.status(statusCode).json({
success: 0,
message: err.message,
stack: err.stack
})
})

//Serve static files if in production
if(process.env.NODE_ENV === 'production'){
//Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) =>{
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
});
}

app.listen(port, () => console.log(`Server started on port ${port}`));
***FRONTEND FETCH***
let response = await fetch('"OUR HEROKU ROUTE"/uploadDesign',{
method:'POST',
body:JSON.stringify(data),
headers:{
'Content-Type':'application/json',
'Accept':'application/json, text-plain */*'
}
})

这个问题是开发人员经常面临的问题,可能是由于各种原因。根据你的问题,我认为你安装并正确使用了cors包。因此,这必须与您的服务器映像api路由有关。部署映像时,存储映像的文件夹是否为空?如果是这样,请尝试在其中添加一个图像,因为如果它是空的,那么该文件夹将不会在heroku服务器中创建。