Node.js : Express app.get with multiple query parameters - node.js

I want to query the yelp api, and have the following route:
app.get("/yelp/term/:term/location/:location", yelp.listPlaces)
When I make a GET request to
http://localhost:3000/yelp?term=food&location=austin,
I get the error
Cannot GET /yelp?term=food&location=austin
What am I doing wrong?

Have you tried calling it like this?
http://localhost:30000/yelp/term/food/location/austin
The URL you need to call usually looks pretty much like the route, you could also change it to:
/yelp/:location/:term
To make it a little prettier:
http://localhost:30000/yelp/austin/food

In the requested url http://localhost:3000/yelp?term=food&location=austin
base url/address is localhost:3000
route used for matching is /yelp
querystring url-encoded data is ?term=food&location=austin i.e. data is everything after ?
Query strings are not considered when peforming these matches, for example "GET /" would match the following route, as would "GET /?name=tobi".
So you should either :
use app.get("/yelp") and extract the term and location from req.query like req.query.term
use app.get("/yelp/term/:term/location/:location") but modify the url accordingly as luto described.

I want to add to #luto's answer. There is no need to define query string parameters in the route. For instance the route /a will handle the request for /a?q=value.
The url parameters is a shortcut to define all the matches for a pattern of route so the route /a/:b will match
/a/b
/a/c
/a/anything
it wont match
/a/b/something or /a

Express 4.18.1 update:
using app.get("/yelp/term/:term/location/:location"), your query string can be yelp/term/food/location/austin
So your request url will look like this:
http://localhost:3000/yelp/term/food/location/austin

Related

flask request path without url

Is there a flask function that returns request url without the variables ?
#app.route('/index/<int:id1>/<int:id2>')
#app.route('/index1/<int:id1>/<int:id2>')
For a request /index/2/1 I expect /index in result
For a request /index1/2/1 I expect /index1 in result
I asked a function like described here https://stackoverflow.com/a/15975041/4772933
thanks
Not sure if this is what you want, but you can add more #app.routes to match your conditions, like:
#app.route('/index/<int:id1>/<int:id2>')
#app.route('/index')
def .....
This way the request /index/2/1 and /index will be maped to the same function
Edit
I think the answer is no. But you can use request.url_rule inside the function to get the pattern matched for url, then should be easy to rip off the variables parts. On your case you will get the string '/index//' as an answer.

expressjs pattern to match the rest of the path

I'm trying to create an endpoint that contains an actual path that I extract and use as a parameter. For instance, in the following path:
/myapi/function/this/is/the/path
I want to match "/myapi/function/" to my function, and pass the parameter "this/is/the/path" as the parameter to that function.
If I try this it obviously doesn't work because it only matches the first element of the path:
app.get("/myapi/function/:mypath")
If I try this it works, but it doesn't show up in req.params, I instead have to parse req.path which is messy because the logic has to know about the whole path, not just the parameter:
app.get("/myapi/function/*")
In addition, the use of wildcard routing seems to be discouraged as bad practice. I'm not sure I understand what alternative the linked article is trying to suggest, and I'm not using the query as part of a database call nor am I uploading any information.
What's the proper way to do this?
You can use wildcard
app.get("/myapi/function/*")
And then get your path
req.params[0]
// Example
//
// For the route "/myapi/function/this/is/my/path"
// You will get output "this/is/my/path"

Cannot GET (Passed url as parameter)

Can routes in express not take a full URL as a parameter?
For example,
router.get("/new/:url", <some function>);
gives me the Cannot GET error when the :url is https://www.google.com
You can't get full URL like this format.This type of format is used to take parameters send by client
router.get("/new/:url", <some function>);
//you can get url as params
req.params.url//Use your URL
You should encode url parameter before sending. Your example encoded would be Http%3A%2F%2Fwww.google.com. On server side you can decode parameter to get value from before.
I think you are not much aware about ExpressJS routing because your url https://www.google.com have // which is used route separation.
In you case, we know that ExpressJS support regex route. I think following regex will work for you
app.get("/new/:protocol(http:|https:|ftp:)?/?/:url", <some function>);
In above case, you have bunded with limited protocol http, https and ftp. You may add more protocol by using | separator( or condition) and even you don't know what would be protocol then you like following
app.get("/new/:protocol?/?/:url", <some function>);
In above both route, ? means option that routes works file for
/new/www.google.com
/new/https://www.google.com
and in your function, you may append protocol in url like
function newUrl(req, res) {
if(req.params.protocol)
req.params.url = req.params.protocol + '//' + req.params.url;
console.log(req.params.url);
}

multiple requests in one URLusing expressJS

I am using Express with Node and I have a requirement in which the user can request the URL as: http://myhost/api/add?mid="mid01"/userID
and I tried this
app.get('/api/:myMedia/:id', function (req, res){
...
})
and tried these req.query for getting mid01 and it didn't work.
I want to have req.params.id and req.query together. How can I handle this?
If your requirement really is http://myhost/api/add?mid="mid01"/userID, it might be a good idea to change that requirement, because it seems really not the right way to do something
But if you really want to do that you should declare your route like app.get('/api/add', ...)
Then with req.query.mid you can get your query value which is "mid01"/userID
Finaly it's up to you to parse that query value to do what you want
But you should not use URL like that, if possible try to use URL in a more standard way, http://myhost/api/add/userID?mid=mid01 or http://myhost/api/add?mid=mid01&path=/userID
To chain requests, use &. http://myhost/api/add?foo1="bar1"&foo2="bar2". This way both of the queries will show up.

Get params using ExpressJS

I'd like to get a specific param using ExpressJS with "#" instead of "?" in the url...
My URL :
http://localhost:3000/#access_token=LMkdfkdmsklmfdkslklmdskfmsda
I'd like to get "access_token" and "req.params.access_token" doesn't work...
Anthony
Short answer: you can't.
Longer answer: fragment identifiers (that's the part after the #) are supposed to be evaluated on the client and are not supposed to be sent to server. Your express app has no way of knowing them.
You could try to convert them to query parameters or path variables (i.e. by handling fragment identifier change in javascript) to make them visible server-side.

Resources