title: Nodejs server sometimes fails to upload files using multer, express, forever
body: <p>I am trying to build a simple file server to receive files from post request with multipart/form-data and I followed the multer tutorial but it seems something is not quite right and it sometimes returns 500 internal server error. The code is in the following.</p>
  
  <pre><code>var express = require('express');
  var multer  = require('multer');
  var mkdirp = require('mkdirp');
  var app = express();
  var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      var dir = 'file/' + req.body.jobid + '/';
      mkdirp(dir, function(err) {
          if(err) {
              console.error(err);
          }
      });
      cb(null, dir);
      console.log("Upload: saved to " + dir + file.originalname);
    },
    filename: function (req, file, cb) {
      cb(null, file.originalname);
    }
  });
  
  var upload = multer({ storage: storage });
  
  app.post('/', upload.single("file"),function (req, res, next) {
    res.status(200).end();
    next(req,res);
  });
  
  var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;
  
    console.log('Example app listening at http://%s:%s', host, port);
  });
  </code></pre>
  
  <p>The error happens like every two requests out of ten.</p>
  
  <p>The code I used to start forever</p>
  
  <pre><code>forever start app.js;
  </code></pre>
  
  <p>I am new to nodejs and this kind of errors (errors that happen occasionally) is killing me.</p>
  
  <p>I added some functions to log the possible errors.</p>
  
  <pre><code>app.use(function(err, req, res, next) {
    console.error(err.stack);
    next(err);
  });
  
  app.use(function(err, req, res, next) {
    if (req.xhr) {
      res.status(500).send({ error: err.stack });
    } else {
      next(err);
    }
  });
  </code></pre>
  
  <p>and it logged something like this:</p>
  
  <pre><code>Upload: saved to file/9974de5b-a9c7-4de2-92d0-5dcf277b3946/file123
  Error: ENOENT, open 'file/9974de5b-a9c7-4de2-92d0-5dcf277b3946/file123'
      at Error (native)
  Error: ENOENT, open 'file/9974de5b-a9c7-4de2-92d0-5dcf277b3946/file123'
      at Error (native)
  </code></pre>