Wildan S. Nahar Wildan S. Nahar - 1 year ago 152
Node.js Question

Express get api with params

I'm new to express API. I have an API with http GET and I'd like to get data from mongodb that matches to certain criteria. Here is my mongodb documents looks like :

{value:"24", category:"low"} {value:"30", category:"middle"}, {value:"23", category:"low"}

And my code in the server side looks like this :

Temp.find(function(err, temps){
} else {
var category = req.params.category
json: function(){
html: function(){
res.render('temps', {
title: 'All temps',
temps: temps

I wanna get all values where the category is
with query like this :
. How can I modify the codes above? Thanks for the help.

Answer Source

If you check for req.params.category then your route should be /temp/:category for example, /temp/low because req.params is for path parameters.

If you want to check for query parameters like /temp?category=low then you should check for req.query.category