Express.js で大きいサイズのファイルを 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 オプションの値を変更して、アップロードするファイルサイズ制限を上げればよいです。
app.use(express.bodyParser({
limit: '1024mb'
}));
app.use('/upload', express.bodyParser({
limit: '1024mb'
}));
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);
}
});
以上、Express.js で Request Entity Too Large エラーが発生しないように express.bodyParser({ limit: fileSize }) を活用していきたい、現場からお送りしました。