title: Create folder before upload in express js
body: <p>How can I create a folder (if the folder does not exist yet) before the image will be uploaded? I always get error <code>ENOENT</code>.</p>
  
  <p>When I try this code:</p>
  
  <pre><code>module.exports = function(router){
  
      router.post('/result', directory.tmp, uploader.single, function(req,res){ 
             //some data manipulation here
      });
  }
  </code></pre>
  
  <p><code>//directory.js</code></p>
  
  <pre><code>module.exports.tmp = function(req, res, next){
          mkdirp('./tmp/' + moment().format('MM-DD-YY') + '/' + moment().format('HH'), function (err) {
              if (err) console.error(err)
              console.log("==================================");
              console.log("tmp folder created");
              console.log("==================================");
          });
          next();
      };
  </code></pre>
  
  <p>Though I used <code>directory.tmp</code> first so it will create a folder if it is not existing, I think <code>uploader.single</code> is executed first that is why I got that error. After receiving the error, then that's the time my app created the folder. So in other words, the file uploaded was not saved. How to fix this so it will create the folder first, then upload the file. Btw, I am using <code>mkdirp</code> and <code>multer</code>.</p>