title: how to get form data value on server while upload file?
body: <p>I have a form in which i have <code>one user input field where user type his name and one type="file" which need to be uploaded on server</code> .I am able to upload file on server
  but I want to fetch request parameter on server (<code>user enter name</code>) .I used <code>body-parser</code> to get value (req.body) , but getting <code>undefined</code></p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;form id="fileUploadForm"&gt;
      &lt;input type="file" name="file" id="fileId"/&gt;
      &lt;input type="submit" value="Upload" name="submit" class="submit"&gt;
      username &lt;input type="text" placeholder="enter first name"  name="username" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>JS client</strong></p>
  
  <pre><code>$('.submit').on('click', function (e) {
              e.preventDefault();
              e.stopPropagation();
              if ($('#fileId').val().length === 0) {
                  alert('please insert file')
              } else {
                  var form = $('#fileUploadForm')[0];
  
                  // Create an FormData object
                  var data = new FormData(form);
                  $.ajax({
                      url: 'http://localhost:3000/upload',
                      type: 'POST',
                      data: data,
                      cache: false,
                      enctype: 'multipart/form-data',
                      dataType: 'json',
                      processData: false, // Don't process the files
                      contentType: false, // Set content type to false as jQuery will tell the server its a query string request
                      success: function (data, textStatus, jqXHR) {
                          if (typeof data.error === 'undefined') {
                              // Success so call function to process the form
                          }
                          else {
                              // Handle errors here
                              console.log('ERRORS: ' + data.error);
                          }
                      },
                      error: function (jqXHR, textStatus, errorThrown) {
                          // Handle errors here
                          console.log('ERRORS: ' + textStatus);
                          // STOP LOADING SPINNER
                      }
                  });
              }
          })
  </code></pre>
  
  <p>my file is uploaded successfully on server , but I am confused on these two lines</p>
  
  <pre><code>  var form = $('#fileUploadForm')[0];
      // Create an FormData object
    var data = new FormData(form);
  </code></pre>
  
  <p>what is in data ? from my point of view data contain <code>username value and file data which need to be uploaded</code></p>
  
  <p><strong>server code</strong></p>
  
  <p>how to fetch <code>username and file data</code> value on server .I already user bodypaser plugin , but req.body is undefined</p>
  
  <p>here is my code</p>
  
  <pre><code>var express = require('express');
  var multer = require('multer');
  var bodyParser = require('body-parser');
  var cors = require('cors');
  var app = express();
  var PORT = process.env.PORT || 3000;
  
  // use of body parser
  app.use(bodyParser.urlencoded({
      extended: true
  }));
  
  app.use(bodyParser.json());
  app.use(cors());
  
  var storage = multer.diskStorage({ //multers disk storage settings
      destination: function (req, file, cb) {
          cb(null, './uploads/')
      },
      filename: function (req, file, cb) {
          var datetimestamp = Date.now();
          cb(null, file.fieldname + '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length - 1])
      }
  });
  var upload = multer({ //multer settings
      storage: storage
  }).single('file');
  
  
  app.post('/upload', function (req, res) {
      console.log(req.body)
      console.log(req)
      upload(req, res, function (err) {
          if (err) {
              res.json({error_code: 1, err_desc: err});
              return;
          }
          res.json({error_code: 0, err_desc: null});
      });
  });
  
  
  app.listen(PORT, () =&gt; {
      console.log(`App is listening to ${PORT}`);
  })
  </code></pre>
  
  <p>how to get form field values on server which is passed from client ?</p>