title: How to send fields and files in the same form submit in NodeJS/Multer?
body: <p>My problem is that I can't get the req.body and req.file object when I post to /new, but everything is fine and the file is being uploaded in my folder. I just can't get access to the req.body and req.file objects. It returns [object Object] when I log req.body in the console and for req.file it logs undefined.</p>
  
  <p>I've done some research and I found that you can't encode multipart/form-data with body-parser, but I need to have it because I can't send files without it.</p>
  
  <pre><code>var router = require('express').Router(),
      multer = require('multer'),
      path = require('path'),
      passport = require('passport'),
      middleware = require('../middleware'),
      User = require('../models/User'),
      Ad = require('../models/Ad');
  
  // Storage setup
  var storage = multer.diskStorage({
    destination: './public/uploads/',
    filename: function(req, file, done) {
      done(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
    }
  });
  
  // Upload setup
  var upload = multer({
    storage: storage,
    limits: {
      fileSize: 3000000
    }
  }).single('banner');
  
  // Check file type
  function checkFileType(file, callback) {
    // Allowed extensions
    var fileTypes = /jpeg|jpg|png|gif/;
    // Check extention
    var extname = fileTypes.test(path.extname(file.originalname).toLowerCase());
    // Check mime type
    var mimeType = fileTypes.test(file.mimetype);
  
    if (mimeType &amp;&amp; extname) {
      return callback(null, true);
    }
    callback(null, false);
  }
  
  router.get('/new', middleware.isLoggedIn, function(req, res) {
    res.render('new');
  });
  
  router.post('/new', middleware.isLoggedIn, function(req, res) {
    var adObj = {
      owner: req.user,
      url: req.user.url,
      paymentType: req.body.paymentType,
      transactionId: '',
      banner: ''
    };
  
    if (adObj.paymentType === 'paid') {
      adObj.transactionId = req.body.transactionId;
    }
  
    upload(req, res, function(err) {
      if (err) {
        req.flash('error', err.message);
        return res.redirect('/new');
      }
      adObj.banner = '/uploads/' + req.file.filename;
    });
  
    var ad = new Ad(adObj);
    ad.save();
  
    req.flash('success', 'Successfully added new ad.');
    res.redirect('/');
  });
  </code></pre>
  
  <pre class="lang-html prettyprint-override"><code>&lt;form class="main-form" action="/new" method="POST" enctype="multipart/form-data"&gt;
        &lt;input type="url" name="url" placeholder="Website URL" required&gt;
        &lt;select class="select-ad-type" name="paymentType"&gt;
          &lt;option value="free" selected&gt;Free Ad&lt;/option&gt;
          &lt;option value="paid"&gt;Sponsored Ad - $10&lt;/option&gt;
        &lt;/select&gt;
        &lt;div class="transaction-section"&gt;
          &lt;p&gt;Send $10 to example@gmail.com and type your transaction id below. Your ad will be displayed in the "SPONSORED ADS" section in less than 24 hours.&lt;/p&gt;
          &lt;input type="text" name="transactionId" placeholder="Transaction Id"&gt;
        &lt;/div&gt;
        &lt;input type="file" name="banner" required&gt;
        &lt;button&gt;&lt;i class="fa fa-plus" aria-hidden="true"&gt;&lt;/i&gt; SUBMIT&lt;/button&gt;
      &lt;/form&gt;
  </code></pre>