I was given the following task:
define the route
GET /api/restaurants
This route must accept the numeric query parameters "page" and "perPage" as well as the string parameter "borough", ie: /api/restaurants?page=1&perPage=5&borough=Bronx. It will use these values to return all "Restaurant" objects for a specific "page" to the client as well as optionally filtering by "borough", if provided.
my code is as follows
app.get('/api/restaurants/:page/:perPage/:borough', (req, res) =>{
console.log("worked")
console.log(req.params.page)
console.log(req.params.perPage)
console.log(req.params.borough)
})
now here is the issue i m encountering, the route works fine if i enter the url in the following format
http://localhost:8000/api/restaurants/1/5/Bronx
however it doesn't work when using the format the instructor requires
http://localhost:8000/api/restaurants?page=1&perPage=5&borough=Bronx
I get "Cannot GET /api/restaurants", if anyone could please shed some light on what i m doing wrong i would appreciate it.
I think you're confusing route parameters and query parameters. (They are not the same.)
You can access query parameters on req.query:
app.get('/api/restaurants', (req, res) => {
console.log(req.query.page);
console.log(req.query.perPage);
console.log(req.query.borough);
});