get specific part of url using regex - node.js

I have a URL
ws://mydomain.com/auth/mZFZN4yc/?rtmpUrl=rtmp://abc.com/live/0q4wwjye
How can i get the auth string "mZFZN4yc" from this.
I have used in NodeJS
req.url.match(/^\/auth\/(.*)$/)
but it is returning this whole part
mZFZN4yc/?rtmpUrl=rtmp://abc.com/live/0q4wwjye

Instead of .* you can use [^\/]+, so you get it all up to the /, like:
req.url.match(/\/auth\/([^\/]+)/)

Related

HTTP trigger azure function won't bind route parameter with encoded slashes

I've created an Azure C# HTTP triggered function with a route url: subscriptions/{token}/t. It works fine for urls such as subscriptions/blah/t but it fails with a 404 for parameters that contain encoded slashes: subscriptions/blah%2fblah/t. Any way around this ?
Before we get into debates, {token} is a URL encoded Base64 string which will naturally contain slashes.
This issue seems to persist.
I found out that it can be resolved by double-escaping the string, that is, applying escaping recursively two times.
token = escape(escape(token));
In .NET you can use URI.EsacpeDataString()
In JS you can use encodeURIComponent()
Note, that single escaping does not work reliably with Azure functions
but it fails with a 404 for parameters that contain encoded slashes: subscriptions/blah%2fblah/t.
It is make sense because 'subscriptions/blah%2fblah/t' is equal to 'subscriptions/blah/blah/t'. I suggest you define your own encode rule for slashes. For example, you could convert all the slashes to '[[-]]'. Your token will be like this 'subscriptions/blah[[-]]blah/t'. After received the token, you could convert the special characters back.
token = token.Replace("[[-]]", "/");

Retrofit2 request paths not working as expected

I have this Retrofit2 service definition:
#GET("/samples/{sampleId}")
Observable<Sample> getSampleById(#Path("sampleId") String sampleId);
and the base URL is http://xxx.xxx.xxx.xxx:8080/sampledb/. I doesn't work. I get 404
However, if I use as base URL this one: http://xxx.xxx.xxx.xxx:8080/ and I define the service this way:
#GET("/sampledb/samples/{sampleId}")
it works properly. Why? I don't want to put sampledb prefix in every request definition.
Retrofit 2.0 comes with new URL resolving concept. Base URL and #Url have not just simply been combined together but have been resolved the same way as what ahref in HTML does instead
Take a look at the image
Base URL: always ends with /
#Url: DO NOT start with /
For more info refer this blog
Try to remove the "/" from your GET annotation: #GET("samples/{sampleId}")

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.

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

ExpressJS Route Parameter with Slash

Im using ExpressJS. I want pass url as parameter.
app.get('/s/:url', function(req, res) {
console.log(req.params.url);
});
/s/sg.com //sg.com
/s/www.sg.com //www.sg.com
/s/http://sg.com //http://sg.com
/s/http://sg.com/folder //http://sg.com/folder
How to correct the route such that everything afterr /s/ will be considered as paramenter including slashes.
Thanks
Uh, if you want to stick a URL inside of another URL, you need to URLencode it. If you want to stick one in their raw and suffer the consequences, just use app.get('/s/*'... and then manually parse out the url with req.url.slice(3). But hear me know and believe me later, URL Encoding is the right way to do this via the encodeURIComponent that is built in to JavaScript and works in both the browser and node.js.

Resources