title: Why is request.body undefined?
body: <p>I have a node-js server which includes bodyparser and everything: </p>
  
  <pre><code>var express = require('express');
  var dbcon = require('./app/db/databaseconnection');
  var bodyParser = require('body-parser');
  
  var app = express();
  var router = express.Router();
  
  var filepath = __dirname + '/views/';
  app.set('view engine', 'ejs');
  app.use(express.static(__dirname + '/public'));
  app.use('/', router);
  app.use(bodyParser);
  app.use(bodyParser.json()); // support json encoded bodies
  app.use(bodyParser.urlencoded({ extended: false })); // support encoded bodies
  app.use('*', (request, response) =&gt; response.render(filepath + '404', { page_name: '404' }));
  
  
  // var results = dbcon.getProducts().then(results =&gt; {
  //      return results;
  //  }).catch(err =&gt; { console.log(err) })
   //ejs.render(store , { results });
  router.get('/', (request, response) =&gt; response.render(filepath + 'index', { page_name: 'home' }));
  router.get('/store', (request, response) =&gt; {
      return dbcon.getProducts().then(results =&gt; {
          return response.render(filepath + 'store', { page_name: 'store', products: results });
      });
  });
  router.get('/product', (request, response) =&gt; {
      return dbcon.getProductDetails().then(results =&gt; {
          return response.render(filepath + 'product', { page_name: 'product', products: results, req: request });
      });
  });
  router.get('/purchase', (request, response) =&gt; {
      return dbcon.getProductDetails().then(results =&gt; {
          response.render(filepath + 'purchase', { page_name: 'purchase', products: results, req: request, res: response });
      });
  });
  router.post('/purchase', (request, response) =&gt; {
      //return dbcon.createCustomer(request.body.fname, request.body.lname, request.body.address, request.body.city, request.body.state, request.body.country, request.body.zip, request.body.phone, request.body.cardnumber, request.body.cardtype, request.body.expirationdate, request.body.securitycode, request.body.cardaddress, request.body.productID);
      console.log(request.body.name);
      response.redirect('/receipt');
  });
  router.get('/receipt', (request, response) =&gt; response.render(filepath + 'receipt', { page_name: 'receipt' }));
  router.get('/about', (request, response) =&gt; response.render(filepath + 'about', { page_name: 'about' }));
  router.get('/contact', (request, response) =&gt; response.render(filepath + 'contact', { page_name: 'contact' }));
  router.get('/build', (request, response) =&gt; response.render(filepath + 'build/build'));
  router.get('/learn', (request, response) =&gt; response.render(filepath + 'learn/learn'));
  
  app.listen(3000, () =&gt; console.log("Server running at Port 3000"));
  </code></pre>
  
  <p>I also have a ejs page which includes a form: </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
      &lt;title&gt;PC Store | Purchase&lt;/title&gt;
      &lt;% include partials/header %&gt;
      &lt;link rel="stylesheet" link="/css/purchaseCanvas.css" /&gt;
      &lt;script src="/js/toggleCountries.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;header&gt;
          &lt;% include partials/navbar %&gt;
      &lt;/header&gt;
      &lt;main&gt;
      &lt;div class="container"&gt;
          &lt;h1 align="center"&gt;Order&lt;/h1&gt;
          &lt;div class="orderproduct center"&gt;
          &lt;% for (var i = 0; i &lt; products.length; i++) { 
              //console.log(products[i]); 
              if (products[i].model == req.query.product) { %&gt;
              &lt;h4&gt;Product: &lt;/h4&gt;
              &lt;img src="/img/productImages/&lt;%= products[i].image01 %&gt;" width="100"&gt;
              &lt;h5&gt;Brand: &lt;%= products[i].brand %&gt;&lt;/h5&gt;
              &lt;h5&gt;Model: &lt;%= products[i].model %&gt;&lt;/h5&gt;
              &lt;h5&gt;Series: &lt;%= products[i].series %&gt;&lt;/h5&gt;
              &lt;h5&gt;Price: $&lt;%= products[i].price %&gt;&lt;/h5&gt;
              &lt;input type="hidden" id="productID" name="productID" value="&lt;%= products[i].productID %&gt;"&gt;
              &lt;hr&gt;
          &lt;% }} %&gt;
          &lt;/div&gt;
          &lt;form action="/purchase" method="post"&gt;
              &lt;div class="canvas"&gt;
                  &lt;br&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="text" class="form-control" id="fname" name="fname" placeholder="First Name" required&gt;
                      &lt;p id="fnameError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="text" class="form-control" id="lname" name="lname" placeholder="Last Name" required&gt;
                      &lt;p id="lnameError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="text" class="form-control" id="address" name="address" placeholder="Shipping Address" required&gt;
                      &lt;p id="addressError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="text" class="form-control" id="city" name="city" placeholder="City" required&gt;
                      &lt;p id="cityError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;select class="form-control" id="country" name="country"&gt;&lt;/select&gt;
                      &lt;p id="countryError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;select class="form-control" id ="state" name="state"&gt;
                          &lt;option value="-1"&gt;Select State&lt;/option&gt;
                      &lt;/select&gt;
                      &lt;p id="stateError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt; &lt;br&gt;
                  &lt;script&gt;populateCountries("country", "state");&lt;/script&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="text" class="form-control" id="zip" name="zip" placeholder="Zip Code" required&gt;
                      &lt;p id="zipError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone Number" required&gt;
                      &lt;p id="phoneError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="text" class="form-control" id="cardnumber" name="cardnumber" placeholder="Card Number" required&gt;
                      &lt;p id="cardnumberError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;select class="form-control" id="cardtype" name="cardtype"&gt;
                          &lt;option value="-1"&gt;Select Card Type&lt;/option&gt;
                          &lt;option value="Visa"&gt;Visa&lt;/option&gt;
                          &lt;option value="MasterCard"&gt;MasterCard&lt;/option&gt;
                          &lt;option value="American Express"&gt;American Express&lt;/option&gt;
                          &lt;option value="Discover"&gt;Discover&lt;/option&gt;
                      &lt;/select&gt;
                      &lt;p id="cardtypeError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt; &lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="date" class="form-control" id="expirationdate" name="expirationdate" placeholder="Expiration Date" required&gt;
                      &lt;p id="expirationdateError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                      &lt;div class="center"&gt;
                      &lt;input type="text" class="form-control" id="securitycode" name="securitycode" placeholder="Security Code" required&gt;
                      &lt;p id="securitycodeError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="text" class="form-control" id="cardaddress" name="cardaddress" placeholder="Billing Address" required&gt;
                      &lt;p id="cardaddressError" class="errorText"&gt;&lt;/p&gt;
                  &lt;/div&gt;&lt;br&gt;
                  &lt;div class="center"&gt;
                      &lt;input type="submit" class="btn btn-primary" value="Order"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
      &lt;/main&gt;
      &lt;footer&gt;
          &lt;% include partials/footer %&gt;
      &lt;/footer&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;   
  </code></pre>
  
  <p>From a post I read that you can use request.body to get the values from the inputs in the form. So I tried to do that but it doesn't work.
  When I do request.body it says undefined and when I try to do request.body.fname it says error and that name doesn't exist. Anyone know what the problem could be? People have stated that it was because of the order of app.use, but I've tried changing it and it still wouldn't work. Please Help! </p>