How fix Unhandled 'error' event with expressjs + multer

Package: "multer": "^1.4.2"











//
var express = require('express');
var router = express.Router();
var multer = require('multer');

var upload = multer({ dest: __dirname + '/../uploads/' });

var customUpload = upload.single('photos');
router.post('/upload', function (req, res, next) {
    req.body = req.body || {};
    req.body.api = req.query.api;
    if (!req.body.api) return res.sendStatus(403);
    if (req.body.api != apiKey) return res.sendStatus(403);
    next();
}, function (req, res, next) {
    customUpload(req, res, function (err) {
        if (err instanceof multer.MulterError) {
            // A Multer error occurred when uploading.
            console.log('MulterError', err);
        } else if (err) {
            // An unknown error occurred when uploading.
            console.log('UnhandledError', err);
        }
        if(err) {
            return res.sendStatus(403);
        }
        // req.files is array of `photos` files
        // req.body will contain the text fields, if there were any
        res.sendStatus(200);
    });
});
//

Why:
events.js:174
      throw er; // Unhandled 'error' event
I hate see this when run nodejs!
I add more code and Everything looks good again:


//
process.on('uncaughtException', function(ls){
    (function(){})();
});
//

And more see your error (can type error):

//
var timeout = require('connect-timeout')
var app = express();
app.use(timeout('9s'));

process.on('uncaughtException', function(ls){
    console.error(ls);
    (function(){})();
});
//


Comments