javascript - 上传图片时出现错误 413 有效载荷太大

标签 javascript node.js reactjs http-status-code-413

我正在尝试使用 从本地上传图像base64 进行图像检测。
并且在本地主机和 postman 中一切正常。
但是在部署之后,我得到了 CROS 错误。
我已经在 server.js 中有 cors 中间件

const express = require("express");    
const cors = require("cors");
const bodyParser = require("body-parser");

const app = express();

app.use(cors());

app.use(bodyParser.json({ limit: "10000kb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "10000kb", extended: true }));
cors 中间件在使用 url 获取图像时工作正常,
但是当我尝试 从本地上传图片通过使用 base64 ,控制台显示:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
这是我尝试过的解决方案:
  • cors-任何地方
  • App.js
    
        const proxyUrl = 'https://cors-anywhere.herokuapp.com/';
        
        fetch(proxyUrl + API_CALL.IMAGE_URL, {
              method: 'post',
              headers: {'Content-Type': 'application/json'},
              body: JSON.stringify({
                inputLink: inputLink,
                inputMethod: inputMethod
              }),
              credentials: 'include'
            })
    
    然后显示 413 payload too large .
    由于在localhost和postman中测试时没有错误,我发现一些文章说它可能仍然是cors错误。
  • CORS 预检

  • 服务器.js
        const corsOptions = {
            origin: 'https://front-end-url/',
            methods: 'GET, POST, PUT',
            credentials: true,
            allowedHeaders: 'Content-Type,Authorization',
            exposedHeaders: 'Content-Range,X-Content- Range'
        };
        app.options('/imageUrl', cors(corsOptions));
    
    它显示错误:
    CORS policy: Response to preflight request doesn't pass access control check: 
    The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' 
    when the request's credentials mode is 'include'
    
  • 在我删除 credentials: 'include' 之后,它显示 413 payload too large再次。

  • 我很困惑......有谁知道如何解决它?谢谢你。

    最佳答案

    最后通过放置来修复错误express.json() 之后 bodyParser .

    像这样:

    app.use(bodyParser.json({limit: '50mb'}));
    app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
    app.use(express.json());
    

    如果先运行 express.json(),express 会将全局限制设置为 1mb。

    对于下一个需要更多细节的人:
    Error: request entity too large
    对于需要设置 Nginx 配置文件的人:
    Increasing client_max_body_size in Nginx conf on AWS Elastic Beanstalk

    关于javascript - 上传图片时出现错误 413 有效载荷太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60947294/

    相关文章:

    javascript - 如何使用for of循环获取对象的值

    javascript - Fabric.js、Darkroom.js 和 devicePixelRatio 偏移量

    javascript - Node.js 上的 RegExp 导致不确定模式

    javascript - 无法读取未定义的属性 'indexOf' - React-sortable-hoc

    javascript - Vue.js:延迟后运行方法,但如果发生用户输入则重置延迟

    javascript - 将原型(prototype)继承给 JavaScript 中的所有子元素

    javascript FileReader 获取名称?

    c# - 加载javascript和速度问题

    javascript - React createRef() 与回调引用。使用一个比另一个有什么优势吗?

    javascript - 如何在 React js 中更改 mapbox-gl 的中心纬度和经度?