• nodejs express 413


    When adding a console.log('Limit file size: '+limit); in node_modules/express/node_modules/connect/lib/middleware/json.js:46 and restarting node, I get this output in the console:

    Limit file size: 1048576
    connect.multipart() will be removed in connect 3.0
    visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
    connect.limit() will be removed in connect 3.0
    Limit file size: 52428800
    Express server listening on port 3002

    We can see that at first, when loading the connect module, the limit is set to 1mb (1048576 bytes). Then when I set the limit, the console.log is called again and this time the limit is 52428800 (50mb). However, I still get a 413 Request entity too large.

    Then I added console.log('Limit file size: '+limit); innode_modules/express/node_modules/connect/node_modules/raw-body/index.js:10 and saw another line in the console when calling the route with a big request (before the error output) :

    Limit file size: 1048576

    This means that somehow, somewhere, connect resets the limit parameter and ignores what we specified. I tried specifying the bodyParser parameters in the route definition individually, but no luck either.

    While I did not find any proper way to set it permanently, you can "patch" it in the module directly. If you are using Express 3.4.4, add this at line 46 of node_modules/express/node_modules/connect/lib/middleware/json.js :

    limit = 52428800; // for 50mb, this corresponds to the size in bytes

    The line number might differ if you don't run the same version of Express. Please note that this is a bad practice and will be overwritten if you update your module.

    So this temporary solution works for now, but as soon as a solution is found (or the module fixed, in case it's a module problem) you should update your code accordingly.

    I have opened an issue on their Github about this problem.

    [edit]

    After some research and testing, I found that when debugging, I added app.use(express.bodyParser({limit: '50mb'}));, but after app.use(express.json());. Express would then set the global limit to 1mb because the first parser he encountered when running the script was express.json(). Moving bodyParser above it did the trick.

    That said, the bodyParser() method will be deprecated in Connect 3.0 and should not be used. Instead, you should declare your parsers explicitely, like so :

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

    In case you need multipart (for file uploads) see this post.

    Second edit

    Note that in Express 4, instead of express.json() and express.urlencoded(), you must require thebody-parser module and use its json() and urlencoded() methods, like so :

    var bodyParser = require('body-parser');
    app.use(bodyParser.json({limit: '50mb'}));
    app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

    If the extended option is not explicitly defined for bodyParser.urlencoded(), it will throw a warning (body-parser deprecated undefined extended: provide extended option). This is because this option will be required in the next version and not be optional anymore. For more info on the extendedoption, please refer to the readme of body-parser.

  • 相关阅读:
    Enterprise Solution 开发框架功能点
    设置Form窗体中的控件的属性
    Enterprise Solution 生成实体数据访问接口与实现类型 Code Smith 6.5 模板文件下载
    Enerprise Solution Main 启动方法源代码
    Enterprise Solution 2.3
    .NET 程序启动调试器 .NET 测试代码耗费时间
    代码拼写检查
    编码原则:不变量/前置条件/后置条件
    金融:收益利率计算器
    技术人生:本周改进计划
  • 原文地址:https://www.cnblogs.com/MDK-L/p/4088857.html
Copyright © 2020-2023  润新知