Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple GET parameter with Express

I'm new to Node.js and Express, I've been working on a RESTful API project, and I'm trying to send a GET request with multiple parameters in the URL:

Here is my route:

/centers/:longitude/:latitude

and here is how I tried to call it:

/centers?logitude=23.08&latitude=12.12

and I also tried

/centers/23.08/12.12

It ends up going to this route:

/centers/

So is my way of writing the endpoint wrong? or the way I'm requesting it?

like image 938
Shawerma Avatar asked Dec 05 '16 05:12

Shawerma


1 Answers

You are not correctly understanding how route definitions work in Express.

A route definition like this:

/centers/:longitude/:latitude

means that it is expecting a URL like this:

/centers/23.08/12.12

When you form a URL like this:

/centers?longitude=23.08&latitude=12.12

You are using query parameters (param=value pairs after the ?). To access those, see this question/answers: How to access the GET parameters after "?" in Express?

For that, you could create a route for "/centers" and then you would access req.query.longitude and req.query.latitude to access those particular query parameters.

like image 132
jfriend00 Avatar answered Sep 27 '22 17:09

jfriend00