title: Pass parameter to Express controller from button click
body: <p>Preface: I am writing a Web App using Node.js, Express, and MongoDB.</p>
  
  <p>When a button is clicked I am trying to pass a parameter via URL from my <code>index.ejs</code> to the Express controller <code>ItemController.js</code> in order to dynamically create a filtered set of data.</p>
  
  <p>The button is defined within <code>index.ejs</code> as follows:</p>
  
  <pre><code>&lt;button type="button" class="btn btn-primary" value="/items/items" onclick="loadItems(value, 'Scripting Languages')"&gt;
        Scripting Languages
  &lt;/button&gt;
  </code></pre>
  
  <p>In my external Javascripts file which houses miscellaneous functions, <code>loadItems</code> executes the following:</p>
  
  <pre><code>function loadItems(page, subcategory) {
      window.history.replaceState({}, '', "?subcat=" + subcategory); //set URL Param
      $('#mainContent').load(page); //load item listing page into grid within HTML GridLayout
  }
  </code></pre>
  
  <p>After this, the router/controller handles the data filtering and page rendering:</p>
  
  <p><strong>Router (item.js)</strong></p>
  
  <pre><code>...
  // Get all items
  router.get('/items', item.items);
  ...
  </code></pre>
  
  <p><strong>Controller (ItemController.js)</strong></p>
  
  <pre><code>...
    // loads item listings
    itemController.items = function(req, res) {
      Item.find({}).exec(function (err, items) {
        if (err) {
          console.log("Error:", err);
        }
        else {
          var URLSubcat = "Scripting Languages"; //this variable needs to change
          var filtered = items.filter(function(el) {
            return el.subcategory === URLSubcat;
          });
          res.render("../views/items/items", {items: filtered});
        }
      });
    };
  ...
  </code></pre>
  
  <p>I have tried using <code>req.query.subcat</code> in order to try and grab the parameter from the URL, however it comes back as undefined. I have also tried the techniques in the links below to no avail. What should I be doing to accomplish this? Is there a better way to go about this?</p>
  
  <p><strong>Previously Tried</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js">How to get GET (query string) variables in Express.js on Node.js?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/17007997/how-to-access-the-get-parameters-after-in-express">How to access the GET parameters after &quot;?&quot; in Express?</a></p>