title: How to pass request query parameters through a NodeJS application
body: <p>I'm currently working on learning NodeJs and am running into a problem with passing query parameters.  The stack consists of express, request-promise, and mongodb.  The design of the application I'm shooting for is to have an <code>express.js</code> that is basically the UI app for users.  There is also a <code>server.js</code> that defines any REST api endpoints, and a <code>recipes.js</code> that handles running all the queries and any logic I would want.</p>
  
  <p>Below is the <code>express.js</code> file that I mentioned before that would handle the UI:</p>
  
  <pre><code>// Node imports.
  const express = require('express');
  const app = express();
  const request = require('request');
  const rp = require('request-promise');
  const url = require('url');
  
  const port = 3001;
  
  // View configuration information.
  app.use(express.static('public'));
  app.set('view engine', 'ejs');
  
  // Routing.
  app.get('/recipes', (req, res) =&gt; {
      console.log("TODO(map) In the express.js: " + req.query.count);
  
      var options = {
      method: 'GET',
      uri: 'http://localhost:3000/recipes',
      json: true
      }
  
      // Run get and wait on promise before rendering.
      rp(options, (req, res) =&gt; {
      }).then((recipes_list) =&gt; {
      res.render('index-promise', { pageTitle: 'Recipes', recipes: recipes_list })
      });
  })
  
  // Server listening.
  app.listen(port, () =&gt; {
      console.log('Server listening on http://localhost:' + port);
  });
  </code></pre>
  
  <p>You can see the app has a single URL route for now.  The <code>server.js</code> file below shows the API endpoints mentioned above:</p>
  
  <pre><code>// Node imports.
  const url = require('url');
  const express = require('express');
  const app = express();
  
  const port = 3000;
  
  // API imports.
  var recipeApi = require('./apis/recipes.js');
  
  // Routing.
  app.get('/recipes', (req, res) =&gt; {
      console.log("TODO(map) In the server.js: " + req.query.count);
      recipeApi.getRecipesByCount(req, res);
  })
  
  // Server listening.
  app.listen(port, () =&gt; {
      console.log('Server listening on http://localhost:' + port);
  });
  </code></pre>
  
  <p>The final file, <code>recipes.js</code>, connect to the database and gets the data an kicks it back.</p>
  
  <pre><code>// Node imports.
  const url = require('url');
  const express = require('express');
  const app = express();
  
  // Mongo imports.
  const MongoClient = require('mongodb').MongoClient;
  const Db = require('mongodb').Db
  const Server = require('mongodb').Server
  
  // Create a DB object so we can query the db anywhere.
  const db = new Db('mydb', new Server('localhost',27017));
  
  /**
   * Function for getting a certain number of recipes.
   * NOTE: The req object should include a "count" query parameter.
   * 
   * req: Request being passed in from the server.js
   * res: Response that will be sent back.
   *
   */
  exports.getRecipesByCount = (req, res) =&gt;
  {
      console.log("TODO(map) In the recipes.js: " + req.query.count);
  
      if (!req.query.count)
      {
      req.query.count = 0
      }
      db.open((err, db) =&gt; {
      db.collection("recipes").find({}).limit(parseInt(req.query.count)).toArray((err, result) =&gt; {
          if (err) throw err;
          res.setHeader('Content-Type', 'application/json');
          res.send(result);
          db.close();
      });
      });
  
  }
  </code></pre>
  
  <p>So at the end of all this, the issue that I'm running into is in the <code>console.log</code> calls both the <code>server.js</code> and the <code>recipes.js</code>.  The <code>req.query.count</code> value is undefined despite the fact that the URL I'm hitting is <code>http://localhost:3000/recipes?count=1</code>.  How am I supposed to pass the information the <code>req</code> object has to the other app (the REST API) so I can get that information to the end point and query the database correctly?</p>