Node.js: Difference between req.query [] and req.params

Is there any difference between getting QUERY_STRING arguments through req.query [myParam] and req.params.myParam? If so, when should I use which one?

Thank.

+100
query-string
Jan 19 '13 at 18:46
source share
5 answers

req.params contains the route parameters (in the part of the URL path) and req.query contains the URL query parameters (after ? in the URL).

You can also use req.param(name) to search for a parameter in both places (as well as req.body ), but this method is now not recommended.

+111
Jan 19 '13 at 19:37
source share

Given this route

 app.get('/hi/:param1', function(req,res){} ); 

and given this URL http://www.google.com/hi/there?qs1=you&qs2=tube

You will have:

REQ. inquiry

 { qs1: 'you', qs2: 'tube' } 

REQ. PARAMS

 { param1: 'there' } 

Express req.params ">

+217
Jan 24 '13 at 18:27
source share

Suppose you defined the name of your route as follows:

 https://localhost:3000/user/:userid 

which will become:

 https://localhost:3000/user/5896544 

Now, if you type: request.params

 { userId : 5896544 } 

So

 request.params.userId = 5896544 

therefore request.params is an object containing properties for the named route

and request.query comes from the query parameters in the URL, for example:

 https://localhost:3000/user?userId=5896544 

request.query

 { userId: 5896544 } 

So

 request.query.userId = 5896544 
+8
May 17 '19 at 10:06
source share

Now you can access the query using dot notation.

If you want to access, say you get a GET request in /checkEmail?type=email&utm_source=xxxx&email=xxxxx&utm_campaign=XX , and you want to extract the used request.

 var type = req.query.type, email = req.query.email, utm = { source: req.query.utm_source, campaign: req.query.utm_campaign }; 

Params are used for a self-determining parameter to receive a request, something like (example):

 router.get('/:userID/food/edit/:foodID', function(req, res){ //sample GET request at '/xavg234/food/edit/jb3552' var userToFind = req.params.userID;//gets xavg234 var foodToSearch = req.params.foodID;//gets jb3552 User.findOne({'userid':userToFind}) //dummy code .then(function(user){...}) .catch(function(err){console.log(err)}); }); 
+7
Aug 24 '17 at 14:48
source share

I want to mention one important point regarding req.query because I am currently working on pagination functionality based on req.query and I have one interesting example to demonstrate to you ...

Example:

 // Fetching patients from the database exports.getPatients = (req, res, next) => { const pageSize = +req.query.pageSize; const currentPage = +req.query.currentPage; const patientQuery = Patient.find(); let fetchedPatients; // If pageSize and currentPage are not undefined (if they are both set and contain valid values) if(pageSize && currentPage) { /** * Construct two different queries * - Fetch all patients * - Adjusted one to only fetch a selected slice of patients for a given page */ patientQuery /** * This means I will not retrieve all patients I find, but I will skip the first "n" patients * For example, if I am on page 2, then I want to skip all patients that were displayed on page 1, * * Another example: if I am displaying 7 patients per page , I want to skip 7 items because I am on page 2, * so I want to skip (7 * (2 - 1)) => 7 items */ .skip(pageSize * (currentPage - 1)) /** * Narrow dont the amound documents I retreive for the current page * Limits the amount of returned documents * * For example: If I got 7 items per page, then I want to limit the query to only * return 7 items. */ .limit(pageSize); } patientQuery.then(documents => { res.status(200).json({ message: 'Patients fetched successfully', patients: documents }); }); }; 

You req.query.pageSize + sign before req.query.pageSize and req.query.currentPage

What for? If you delete + in this case, you will get an error, and this error will be generated, because we will use an invalid type (with the error message, the "limit" field must be numeric).

Important Note : By default, if you extract something from these query parameters, it will always be a string , because it follows the URL and is treated as text.

If we need to work with numbers and convert query operators from text to numbers, we can simply add a plus sign in front of the operator.

0
Apr 08 '19 at 19:42
source share



All Articles