本文介绍了如何修复Node.js中的'413 Request Entity Too Large'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试允许用户将多个图像上传到我的网站,然后将这些图像作为附件通过电子邮件发送给所有用户,但是我遇到一个错误,指出文件大小太大.

我正在使用Mailgun发送邮件,使用Cloudinary上传/存储图像,使用MongoDB作为我的数据库,使用request.js请求图像,使用Cloud 9进行开发,并使用Node.js/Express作为我的后端. /p>

用户流程如下:

  • 用户将图片提交到网站上
  • 图片通过Cloudinary上传,每个图片的直接链接保存在MongoDB数据库中
  • 邮件通过Mailgun发出通知,通知用户该新帖子带有添加为附件的图像

我正在使用request.js从Cloudinary请求图像,然后将每个图像推入一个数组,然后将其添加为Mailgun的附件参数.

这对于较小的图像效果很好,但是当用户上传大量的高质量图像时,我会收到错误消息.

我尝试为Multer,bodyParser.json和bodyParser.urlencoded以及100MB的parameterLimit添加100MB的文件大小限制.

var upload = multer({ storage: storage, fileFilter: imageFilter, limits: {fileSize: 100000000}});

app.use(bodyParser.json({limit: "100mb", parameterLimit: 100000000}));
app.use(bodyParser.urlencoded({limit: '100mb', extended: true, parameterLimit: 100000000}));

var images = [];
post.images.forEach(function(photo){
    images.push(request(photo));
});

var data =  {
    from: "email <[email protected]>",
    to: "[email protected]",
    subject: 'this is an email',
    html: 'this is an email',
    attachment: images
};

预期结果是成功发送一封包含所有图像的电子邮件.

实际结果是此错误消息:

{ Error: <html>
<head><title>413 Request Entity Too Large</title></head>
<body bgcolor="white">
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx</center>
</body>
</html>

at IncomingMessage.res.on (/home/ubuntu/workspace/TW/node_modules/mailgun-js/lib/request.js:319:17)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9) statusCode: 413 }
解决方案

问题最终根本不是nginx,我试图通过Mailgun通过电子邮件将图像作为附件发送,并且硬限制为25MB.

在用户可以上传图像的部分中编辑此行之后,一切运行正常:

let result = await cloudinary.v2.uploader.upload(file.path, {width: 1280, height: 720, crop: "limit"});

I am attempting to allow users to upload several images to my website which will then get emailed to all users as attachments but I am coming across an error stating the file size is too large.

I am using Mailgun to send the mail, Cloudinary to upload/store the images, MongoDB as my database, request.js to request the images, Cloud 9 to develop in, and Node.js/Express as my backend.

The user process goes like this:

  • User submits pictures onto the site
  • Pictures are uploaded via Cloudinary and the direct link to each image is saved in the MongoDB database
  • Mail goes out via Mailgun to inform users of the new post with the images added as attachments

I am using request.js to request the images from Cloudinary and then pushing each image into an array which is then added as an attachment parameter for Mailgun.

This works great for smaller images, but when a user uploads a larger batch of high-quality images I receive the error.

I have tried adding a 100MB fileSize limit to Multer, bodyParser.json, and bodyParser.urlencoded as well as a parameterLimit of 100MB.

var upload = multer({ storage: storage, fileFilter: imageFilter, limits: {fileSize: 100000000}});

app.use(bodyParser.json({limit: "100mb", parameterLimit: 100000000}));
app.use(bodyParser.urlencoded({limit: '100mb', extended: true, parameterLimit: 100000000}));

var images = [];
post.images.forEach(function(photo){
    images.push(request(photo));
});

var data =  {
    from: "email <[email protected]>",
    to: "[email protected]",
    subject: 'this is an email',
    html: 'this is an email',
    attachment: images
};

The expected results are a successful email being sent with all of the images attached.

The actual result is this error message:

{ Error: <html>
<head><title>413 Request Entity Too Large</title></head>
<body bgcolor="white">
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx</center>
</body>
</html>

at IncomingMessage.res.on (/home/ubuntu/workspace/TW/node_modules/mailgun-js/lib/request.js:319:17)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9) statusCode: 413 }
解决方案

The problem ended up not being nginx at all, I was trying to email the images as attachments via Mailgun and that has a hard limit of 25MB.

After editing this line in the section where users can upload images everything works perfectly:

let result = await cloudinary.v2.uploader.upload(file.path, {width: 1280, height: 720, crop: "limit"});

这篇关于如何修复Node.js中的'413 Request Entity Too Large'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:14