zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    How To Change Database Name
    Oracle备份与恢复案例
    Linux 下安装 Oracle9i
    在 Linux x86 上安装 Oracle 数据库 10g_1
    在 Linux x86 上安装 Oracle 数据库 10g_2
    Oracle9i数据库DataGuard实施及维护手册2
    在 Linux x86 上安装 Oracle 数据库 10g_3
    流程企业(钢铁企业)的制造执行系统
    理解和使用Oracle分析工具LogMiner
    Oracle9i数据库Data Guard实施及维护手册 1
  • 原文地址:https://www.cnblogs.com/MDK-L/p/4088857.html
Copyright © 2011-2022 走看看