文件太大error handlingNodejs Express Multer

我用ExpressJS使用Muller。 我想限制file upload到0.5 MB。

var limits = { fileSize: 0.5 * 1024 * 1024 }; var upload = multer({ dest: './public/uploads/', limits: limits }).single('upl') app.post('/',upload, function (req, res) { upload(req, res, function (err) { if (err) { console.log("cs " + err); return } res.end('You new avatar is uploaded') // Everything went fine }) }) 

当文件在0.5 MB以下,我可以看到“你新的头像上传”,但是当它结束时,

 Error: File too large at makeError (C:\Users\Owner\Desktop\nodejs projects\dummy\node_modules\multer\lib\make-error.js:12:13) at abortWithCode (C:\Users\Owner\Desktop\nodejs projects\dummy\node_modules\multer\lib\make-middleware.js:77:22) at FileStream.<anonymous> (C:\Users\Owner\Desktop\nodejs projects\dummy\node_modules\multer\lib\make-middleware.js:139:11) at emitNone (events.js:80:13) at FileStream.emit (events.js:179:7) at PartStream.onData (C:\Users\Owner\Desktop\nodejs projects\dummy\node_modules\busboy\lib\types\multipart.js:220:18) at emitOne (events.js:90:13) at PartStream.emit (events.js:182:7) at readableAddChunk (_stream_readable.js:153:18) at PartStream.Readable.push (_stream_readable.js:111:10) 

显然我不希望最终用户看到这一点。 我如何正确处理这个问题?

       

网上收集的解决方案 "文件太大error handlingNodejs Express Multer"