LoginSignup
38
21

More than 5 years have passed since last update.

Express4でエラー「request entity too large」が発生する

Last updated at Posted at 2015-05-31

状況

下記コードで表題のエラーが発生しました。

var fs = require("fs");
var writer= function(req, res, next) {
    console.log(req.body.text);
  fs.writeFile("./out/"+req.body.filename,req.body.text,function(err){
      if(err){
          throw err;
      }
      res.send("書き込み成功");
  })
}

module.exports=writer;

POSTで受け取ったテキストをファイルに出力する単純なプログラムです。
渡した値は1200行程度のテキスト。サイズとしては80kbにも満たない程度です。

対処

この程度のサイズでtoo largeといわれるのは納得がいかなかったのですが、
とりあえず下記のようにすることでエラーを回避することができました。

app.js

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

原因

どうやらbodyParseの設定のようですね。
bodyParserのドキュメントを見てみました
こう書いてあります。

Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Defaults to '100kb'.

resuest bodyのサイズのデフォルトは100kbに設定されているようです。意外と小さいですね。
私が遭遇したケースも100kb超えてたってことなんでしょうね。

38
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
21