'How to pass request query parameters through a NodeJS application
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 express.js that is basically the UI app for users. There is also a server.js that defines any REST api endpoints, and a recipes.js that handles running all the queries and any logic I would want.
Below is the express.js file that I mentioned before that would handle the UI:
// 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) => {
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) => {
}).then((recipes_list) => {
res.render('index-promise', { pageTitle: 'Recipes', recipes: recipes_list })
});
})
// Server listening.
app.listen(port, () => {
console.log('Server listening on http://localhost:' + port);
});
You can see the app has a single URL route for now. The server.js file below shows the API endpoints mentioned above:
// 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) => {
console.log("TODO(map) In the server.js: " + req.query.count);
recipeApi.getRecipesByCount(req, res);
})
// Server listening.
app.listen(port, () => {
console.log('Server listening on http://localhost:' + port);
});
The final file, recipes.js, connect to the database and gets the data an kicks it back.
// 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) =>
{
console.log("TODO(map) In the recipes.js: " + req.query.count);
if (!req.query.count)
{
req.query.count = 0
}
db.open((err, db) => {
db.collection("recipes").find({}).limit(parseInt(req.query.count)).toArray((err, result) => {
if (err) throw err;
res.setHeader('Content-Type', 'application/json');
res.send(result);
db.close();
});
});
}
So at the end of all this, the issue that I'm running into is in the console.log calls both the server.js and the recipes.js. The req.query.count value is undefined despite the fact that the URL I'm hitting is http://localhost:3000/recipes?count=1. How am I supposed to pass the information the req object has to the other app (the REST API) so I can get that information to the end point and query the database correctly?
Solution 1:[1]
Looks like you're sending a request to the backend API on port 3000 with no query params. In your express.js file you have:
var options = {
method: 'GET',
uri: 'http://localhost:3000/recipes',
json: true
}
Which, as you can see, has NO query parameters. You need to add the params here. If you change this to the code below it should work:
var options = {
method: 'GET',
uri: 'http://localhost:3000/recipes?count=' + req.query.count,
json: true
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jordan Kasper |
