NodeJS defining param name of glob value in url - node.js

Hi there I'm defining my get as below:
app.get('/list/:productType/*', middleware.isAuthenticated, function (req, res) {
});
Using this I am able to get the value of productType by calling the req.params.productType. The question is, however. How to do get the rest of the URL defined by the * global rule in the same way that I'd grab the productType parameter.
Thanks for your help!

I don't think there is a direct way to achieve this, you have two options
1) you can be more specific in defining the route e.g. '/list/:productType/:version/' and access the parameter using req.params.version
2) you can use req.url property to get the full request url and manually parse path after /:productType.

I worked out the answer: When console logging req.params the object has the "wildcard" string in the root index of this object. Thanks guys!

Related

How to access route parameters in node.JS?

I want to access the route parameters in Node.JS and I am using the following syntax:
app.get("posts/:postName", function(req,res){
console.log(req.params.postName)
});
Is something wrong in the syntax? The error I get in the browser is, "Cannot GET /posts/print". Over here, print was the key-word I chose.
If you're using Express (I think so for your syntax), you're doing it right:
http://expressjs.com/en/api.html#req
Could you paste more code or create an online example to see If you're instantiating correctly express?
Your code is missing /
app.get("/posts/:postName", function(req,res){
console.log(req.params.postName)
});
make sure you have one get rout if dont what to use "?" at the end this will make sure your this value of post name is in the rout or not and them put them in a sequence like
app.get("posts/:postName?", ( req, res ) => {})
as first rout is with no data in rout and rout below has data in rout with specific attribute
To access route parameter you have to use req.params.parameterName and access query string you have to use req.query.parameterName
Please modify your route like this -
app.get("/posts/:postName", function(req,res){
console.log(req.params.postName)
});
Suppose if you are trying to access page and limit from url like this http://example.com/**?page=2&limit=20**, then for page use req.query.page and for limit use req.query.limit

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"

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 a website url when routing

Sorry, my vocabulary is very limited, any help clarifying this question is deeply appreciated.
I'm building a server using Nodejs and Express, it has a route like /new/:url. I access the value passed on the url by using req.params.url. This works well for simple strings, like chocolate, however, if I pass a website url, like http://www.google.com, then it won't be routed to /new/:url.
Question: how can I pass a website url and access it with Node/Express?
Edit: I am trying to use the GET method, and apparently a way to solve this problem is through Wildcards/Regex.
Thank you very much for helping!
Use Post method.
Set header "Content-Type" : "application/json"
Set body { "urlblahblah~" : "https://www.google.com" }
Then parse It as JSON in server-side
you can use Javascripts encodeURIComponent, so when you passing to your server on client, you will allsways encode the url, so you can pass it as regular parameter. or as mentioned by Hulk if posting is an options you can pass it as body param as well...
var url = encodeURIComponent("http://some.url/asdasa?asdas=12312")
will result in :
"http%3A%2F%2Fsome.url%2Fasdasa%3Fasdas%3D12312"
which is safe for passing as param
The solution that worked for me was Regex. Instead of routing as /new/:url, I used:
/new/:url(*)
So that if http://www.google.com is given as a parameter:
req.params.url = "http://www.google.com"

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

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

Resources