请求太重时的 NodeJS CORS 错误(文件上传)

Bob*_*bby 3 javascript api node.js cors express

我正在使用一个 Angular 前端和一个 NodeJS API,它们实际上与一些 aws 服务(S3 和 Elastic Beanstalk)一起在 prod 中运行。

当我上传看起来很重的图像或上传超过两个图像,每个图像占用 200Ko 时,我实际上会收到这个 CORS 错误。

CORS 错误

当然,我已经设置了我的标题,我不应该对 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();
});
Run Code Online (Sandbox Code Playgroud)

谢谢 !

rut*_*a k 5

当在 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;。您可以将限制更改为适合您的任何内容。