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.

时间: 2024-11-07 21:45:07

nodejs express 413的相关文章

Cannot read property 'Store' of undefined nodejs express session

Express在使用mongodb的时候app配置出错!  "Cannot read property 'Store' of undefined" 原因主要是express版本4++问题 //settings.js module.exports={ cookieSecret:"xxxx", db:"dbname", host:"localhost", } //app.js var express = require("

Nodejs+express+mysql+百度BAE部署node后台

转载请注明出处:http://www.cnblogs.com/shamoyuu/p/node_bae.html 百度有一个应用引擎,价格非常便宜,Java的tomcat每天4毛钱,node每天2毛钱,我以前在上面搭建过一个JavaWeb的项目,今天来说说怎么搭建nodejs+express+mysql的后台. 首先打开console.bce.baidu.com,注册登录完成,然后新建一个应用引擎BAE如下图进行设置 目前基础版的BAE只支持node4.4.4,不过应该已经足够了.如果是用koa2

使用nodejs+express+mongodb开发web的例子

介绍:简单的介绍下nodejs+express+mongodb这三者. nodejs:是运行在服务器端的程序语言,表面上看过去就是javascript一样的东西,实际是由c++开发是服务器语言. express:是nodejs的一种web框架,node有很多的开源框架,express是一个大神开发的(这尊神已经移驾到go语言的开发去了).express可以让你更方便的操作node(因为直接用nodejs写web比较麻烦,nodejs是事件驱动的,所以有很多异步回调,写多了就看着晕...) mon

nodejs+express+ejs+mongoose实例

nodejs+express+ejs+mongoose实例 nodejs学得异常痛苦,在这里将学的东西做一番整理,算是自我安慰吧.根据网上todo示例,用express和mongoose重写了部分代码,主要是业务逻辑这块(CRUD),这个没什么难度.目前尚未解决的问题是:express不能使用ejs layout template,查了好久也没解决,知道的麻烦告诉我一下. 一.代码目录 二.第三方模块 1.express (1)express中文入门指引手册 (2)nodejs中文电子书 (3)

nodejs+Express 开发应用--初始化

nodejs+Express 开发应用--初始化 全局安装express $ npm install express -g 如果以前已经安装过express的话就不用执行这一步骤了. 初始化应用 初始化一个支持session,使用ejs模版的名称为myapp的web应用: $ express --sessions --ejs myapp 执行完毕后根据提示,继续执行 cd myapp npm install [注]:express –help 查看express全部的命令 设置cookie的密钥

NodeJS+Express+MySQL开发小记(2):服务器部署

http://borninsummer.com/2015/06/17/notes-on-developing-nodejs-webapp/ NodeJS+Express+MySQL开发小记(1)里讲过在本地搭建 NodeJS 网站的若干细节.本人最近在阿里云服务器上面按最低配租了4个月的云服务器,所以想试着把这个项目部署到云上.云服务器操作系统是Ubuntu 14.04 LTS.之前一直在Windows下做开发,对于Linux下的环境搭建.配置还不是很熟悉,搭建的过程中学到很多东西. 本文简单记

Nodejs express中创建ejs项目,解决express下默认创建jade,无法创建ejs问题

最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了, 书上命令为: ? 1 express -t ejs microblog 可是执行后,仍旧创建的是jade项目. 原来,express3.x,express4.x中创建ejs命令更新为: express -e microblog //即ejs,-j(即jade)  当然,最直接的,你也可以修改package.json里的定义来实现安装ejs. PS:建立工程过程 1.必须得安装expre

安装nodeJs静态服务器(NodeJs Express MVC 框架)

安装 NodeJs Express MVC 框架 新建项目文件夹 打开cmd 执行以下操作: 一.使用Express框架 1)安装express3 $: npm install -g [email protected] 2)目录下新建工程 $: express -e staticServer 3)安装所需模块 $: cd staticServer && npm install 这部执行完毕即可启动服务器 $: node app express默认public目录为静态资源目录,可在浏览器中

nodejs+express+jade安装备忘

安装步骤 1.安装nodejs,比如安装在E:\nodejs. 确保有两个环境变量 用户环境变量:C:\Users\Administrator\AppData\Roaming\npm 系统环境变量:e:\nodejs 2.安装Express,用稳定版3.5.0,网上资料也大多以这个版本为主,不指定版本的话,默认安装最新版 运行cmd -> npm install -g [email protected] 如果是4.X版本,再安装npm install -g express-generator 3