Answer the question
In order to leave comments, you need to log in
How to catch errors when using Multer in node.js?
Tell me please.
According to the documentation multer
limits - An object that sets limits. Multer passes this object directly to busboy, so see the busboy methods page for details.
I set a limit of 1 mb when a larger file is loaded - throw error in this busboy, and node.js sends a large error page to the client.
How can I catch the error when this exception occurs on the file size, so that I can send my text?
'use strict';
const express = require("express");
const multer = require("multer");
const fs = require("fs");
const app = express();
const storageConfig = multer.diskStorage({
destination: (req, file, cb) =>{
cb(null, "uploads");
},
filename: (req, file, cb) =>{
cb(null, file.originalname);
}
});
const myfilelimits = {fileSize : 1048576};
app.use(express.static(__dirname));
var myupload = multer({storage:storageConfig, limits: myfilelimits}).single("file")
app.use(myupload);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post("/upload", function (req, res, next) {
let filedata = req.file;
console.log(filedata);
if(!filedata) {
res.send("Ошибка при загрузке файла");
}
else {
// res.send("Файл загружен");
filedata.uploadok = 'ok'
res.send (filedata)
}
myupload(req, res, function (err) {
if (err instanceof multer.MulterError) {
// Случилась ошибка Multer при загрузке.
console.log('Случилась ошибка Multer при загрузке.');
} else {
// При загрузке произошла неизвестная ошибка.
console.log('При загрузке произошла неизвестная ошибка.');
}
// Все прекрасно загрузилось.
console.log('Все прекрасно загрузилось');
})
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question