[Node.js][Express] Error: Request Entity Too Large


Express で大きいサイズのファイルをPOSTしたら Request Entity Too Large というエラーが発生した。

エラーメッセージ

Error: Request Entity Too Large
  at Object.exports.error (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/utils.js:63:13)
  at limit (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/middleware/limit.js:51:47)
  at multipart (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/middleware/multipart.js:102:5)
  at /u/apps/com/shared/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:57:9
  at urlencoded (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:52:72)
  at /u/apps/com/shared/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:55:7
  at json (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/middleware/json.js:54:55)
  at Object.bodyParser [as handle] (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5)
  at next (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/proto.js:193:15)
  at Object.cookieParser [as handle] (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
  at next (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/proto.js:193:15)
  at Object.expressInit [as handle] (/u/apps/com/shared/node_modules/express/lib/middleware.js:30:5)
  at next (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/proto.js:193:15)
  at Object.query [as handle] (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/middleware/query.js:44:5)
  at next (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/proto.js:193:15)
  at Function.app.handle (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/proto.js:201:3)
  at Server.app (/u/apps/com/shared/node_modules/express/node_modules/connect/lib/connect.js:65:37)
  at Server.EventEmitter.emit (events.js:98:17)
  at HTTPParser.parser.onIncoming (http.js:2108:12)
  at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
  at Socket.socket.ondata (http.js:1966:22)
  at TCP.onread (net.js:527:27)

bodyParser の limit オプションの値を変更して、アップロードするファイルサイズ制限を上げればよいです。

全 routing に適用する書き方

app.use(express.bodyParser({
  limit: '1024mb'
}));

/upload に適用する書き方

app.use('/upload', express.bodyParser({
  limit: '1024mb'
}));

特定の URL だけ limit を変更する書き方

var defaultBodyParser = express.bodyParser({
  limit: "5mb"
});
var extendedBodyParser = express.bodyParser({
  limit: "1024mb"
});
 
app.use(function(req, res, next) {
  if (req.url === '/upload') {
    return defaultBodyParser(req, res, next);
  } else {
    return extendedBodyParser(req, res, next);
  }
});

以上です。


参考情報

  1. コメントはまだありません。

  1. トラックバックはまだありません。