0

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.

1
  • Well the pattern /api/restaurants/:page/:perPage/:borough does not match on /api/restaurants- You would need to explicitly specify a route for that path. Commented Jan 19, 2022 at 15:40

1 Answer 1

0

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.