title: Multer image upload in Nodejs and Express
body: <p>I've been trying for days to upload an image using Multer. This is how  far I got. I tried multiple things, and can't seem to make it work. I don't know how to use Multer to upload an image in my createUser function in the format shown below. </p>
  
  <p>My  ' server.js ' file:</p>
  
  <pre><code>var express = require('express');
  var multer = require('multer');
  var upload = multer({dest: 'uploads/'});
  var router = require('./app/routes');
  var bodyParser = require('body-parser');
  var mongoose = require('mongoose');
  var session = require('client-sessions');
  var DB_URI = "mongodb://localhost:27017/portfolio";
  var app = express();
  var path = require('path');
  var fs = require('fs');
  app.set('view engine', 'ejs');
  app.use(bodyParser.urlencoded({extended:false}));
  app.use(express.static(__dirname+ '/public'));
  app.use(session({
    cookieName: 'session',
    secret: 'random_string_goes_here',
    duration: 30 * 60 * 1000,
    activeDuration: 5 * 60 * 1000,
  }));
  
  mongoose.connect(DB_URI);
  app.use(router);
  
  app.listen(8080, function(){
      console.log("server is listening on port 8080");
  })
  </code></pre>
  
  <p>My  ' routes.js '</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var projectController = require('./controllers/projectController');
  var userController = require('./controllers/userController');
  router.post('/regUser', userController.createUser);
  </code></pre>
  
  <p>My ' index.ejs '</p>
  
  <pre><code>&lt;form class="form inv" method="POST" action="/regUser" id="reg_form"&gt;
            &lt;h3 style="margin-bottom:40px;"&gt;Register&lt;/h3&gt;
            &lt;!-- &lt;input type="file" name="file"/&gt; --&gt;
            &lt;input type="text" class="form-control" name="name" placeholder="Name"/&gt;
            &lt;input type="text" class="form-control" name="username" placeholder="Username"/&gt;
            &lt;input type="text" class="form-control" name="email" placeholder="Email"/&gt;
            &lt;input type="password" class="form-control" name="password" placeholder="Password"/&gt;
            &lt;input type="file" name="userPhoto"/&gt;
            &lt;input type="submit" class="btn btn-default" value="Submit"/&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>My userController.js</p>
  
  <pre><code>createUser: function(req, res){
  //add code to create user AND upload image
  }
  </code></pre>