我正在使用 Angular 前端和 NodeJS API,这些 API 与一些 aws 服务(S3 和 Elastic Beanstalk)一起实际运行在产品中。
当我上传一张看起来很重的图片或上传两张以上的图片时,我实际上收到了这个 CORS 错误,每张图片需要 200Ko。
当然,我已经设置了我的标题,我不应该对 CORS 有任何问题,问题在于一些 req 大小限制。
我的 app.js 有一些部分。
app.use(bodyParser.json({}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use("/images", express.static(path.join("images")));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS"
);
next();
});
谢谢 !
最佳答案
在 Node 之前使用像 Nginx 这样的网络服务器时,所有请求都通过 Nginx 并在到达 Node.js 之前由 nginx 验证。
Nginx 默认有一个配置文件位于 /etc/nginx/nginx.conf
.这个文件有很多用于所有请求的默认属性,其中之一是 client_max_body_size
.该属性的默认值为 1M (1 MB),您的文件很可能超过了此限制。
检查 nginx 的日志以确定 /var/log/nginx/error.log
.您应该看到以下错误 client intended to send too large body
当下。
为了解决这个问题,只需将以下内容放入您的 nginx.conf
:client_max_body_size 10M;
.您可以将限制更改为适合您的任何内容。
关于javascript - 请求太重时出现 NodeJS CORS 错误(文件上传),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61762349/