Error handling when uploading file using multer with expressjs

你。 提交于 2019-12-18 05:15:35

问题


I am using multer to save the file on server developed through express & nodejs.

I am usign following code.

var express = require('express'),
    multer  = require('multer')

var app = express()

app.get('/', function(req, res){
  res.send('hello world');
});

app.post('/upload',[ multer({ dest: './uploads/'}), function(req, res){

    res.status(204).end()
}]);

app.listen(3000);

Multer saves the file for me in the specified destination folder.

All this is working fine but I have following questions:

  1. If the file saving fails for various reasons, it looks like my route will always return status 204.
  2. I am not sure if status 204 is retured after file is saved or while the file is getting saved asynchronously, status 204 is returned.

回答1:


You can handle errors using the onError option:

app.post('/upload',[
  multer({
    dest    : './uploads/',
    onError : function(err, next) {
      console.log('error', err);
      next(err);
    }
  }),
  function(req, res) {
    res.status(204).end();
  }
]);

If you call next(err), your route handler (generating the 204) will be skipped and the error will be handled by Express.

I think (not 100% sure as it depends on how multer is implemented) that your route handler will be called when the file is saved. You can use onFileUploadComplete to log a message when the upload is done, and compare that to when your route handler is called.

Looking at the code, multer calls the next middleware/route handler when the file has been uploaded completely.




回答2:


Try this

var upload = multer().single('avatar')

app.post('/profile', function (req, res) {
  upload(req, res, function (err) {
    if (err) {
      // An error occurred when uploading 
      return
    }

    // Everything went fine 
  })
}

ref :

http://wiki.workassis.com/nodejs-express-get-post-multipart-request-handling-example/

https://www.npmjs.com/package/multer#error-handling




回答3:


This is how to write multer middleware that handle upload and errors

const multer = require("multer");

function uploadFile(req, res, next) {
    const upload = multer().single('yourFileNameHere');

    upload(req, res, function (err) {
        if (err instanceof multer.MulterError) {
            // A Multer error occurred when uploading.
        } else if (err) {
            // An unknown error occurred when uploading.
        }
        // Everything went fine. 
        next()
    })
}



回答4:


As you can see from the code below (the source from the muter index.js file), if you not pass the onError callback the error will be handled by express.

    fileStream.on('error', function(error) {
      // trigger "file error" event
      if (options.onError) { options.onError(error, next); }
      else next(error);
    });


来源:https://stackoverflow.com/questions/30838901/error-handling-when-uploading-file-using-multer-with-expressjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!