title: Express middleware (multer) in seperate file
body: <p>I was able to move a very simple middleware (isAuthenticated) over to an external middleware file, but I'm having a harder time with moving my multer upload over. I just learned how to move them to seperate files, so it's probably obvius.</p>
  
  <pre><code>routes/index.js
  var middleware = require('../middleware/common');
  var isAuthenticated = middleware.isAuthenticated;
  var upload = middleware.multerSetup;
  ...
  
  router.post('/updateuser',
      upload,
  ...,
      function (req, res, next) {
          res.redirect('/dashboard');
      }
  );
  </code></pre>
  
  <p>--</p>
  
  <pre><code>//middleware/common.js
  var multer = require('multer');
  
  
  Middleware = {
          //Checks whether user is logged in
      isAuthenticated: function(req,res,next){
              if(req.isAuthenticated()){
                  return next();
              }
              req.flash('auth',"You do not have the proper permissions to access this page");
              res.redirect('/');
          },
      multerSetup: function(req,res,next){
          var storage = multer.diskStorage({
              destination: function (req, file, cb) {
                  cb(null, './public/uploads/')
              },
              //detects file type, then adds extension to match file type
              filename: function (req, file, cb) {
                  var ext = "";
                  switch(file.mimetype){
                      case 'image/png':
                          ext = '.png';
                          break;
                      case 'image/jpeg':
                          ext = '.jpeg';
                          break;
                      default:
                          ext = '';
                  }
                  cb(null, Date.now() + ext); //Appending .jpg
              }
          });
  
  
          var upload = multer({storage:storage, fileFilter: function (req, file, cb) {
              var acceptedExt = ['.png','.jpg','.gif','.bmp'];
              if (req.hasOwnProperty('file') &amp;&amp; acceptedExt.indexOf(path.extname(file.originalname))=== -1) {
                  return cb(new Error('Image type not allowed: ' + path.extname(file.originalname)));
              }
  
              cb(null, true)
          }});
  
          return upload.single('upl');
       }
  
  };
  
  module.exports = Middleware;
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>routes\index.js:108
      upload.single('upl'), 
             ^
  
  TypeError: upload.single is not a function
      at Object.&lt;anonymous&gt; (C:\Users\Tyler\WebstormProjects\volunteer\volunteerApp\routes\index.js:108:12)
  </code></pre>