B4J Question URL Routing?

ilan

Expert
Licensed User
Longtime User
hi

i would like to know how i can in the Server Handler map a dynamic url. something like this:

B4X:
    'app.get('/users/:userId/books/:bookId',function (req,res){
    '    //...
    '})

url to call it: http://localhost:3000/users/34/books/8989

now i know i can add the * at the end of the handler url and do it like this:

B4X:
srvr.AddHandler($"/users/*"$, "books", False)

and then call something like this:


and get the values like this:

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Log(req.GetParameter("userid"))
    Log(req.GetParameter("bookid"))
    resp.Write("processing...")
End Sub

but my question is can i do it without adding the value keys in the url?

so i want to reach the handler with this URL: http://localhost:3000/users/34/books/8989
and not with this: http://localhost:3000/users?userid=0081&bookid=0021

this is what i found but cannot figure out what i need to do.

thanx, ilan
 

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
you need to parse the commandstring by yourself.
See

yes, this solution is already known to me as I wrote above but according to the Stackoverflow thread we can edit the URL pattern in the web.xml file and maybe we can allow adding ":" to the URL and still direct to the correct server handler.

the question is can we edit the web.xml file? would that be the solution for this problem?
if not i still can parse the URL by myself.

thanx
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
With the /users endpoint you can grab this url. The * is only for the endpoints not for the parameters

Anything beyond the ? Will get a parameter with req.getparameters

yes i understand that but is there a way to do it without adding the key & value inside the url?
like we can do it in NodeJS for example like this:

B4X:
    'app.get('/users/:userId/books/:bookId',function (req,res){
    '    //...
    '})

and call the url like this: http://localhost:3000/users/34/books/8989
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
No. Jetty only has the * and can only be used once per endpoint.

ok thank you @EnriqueGonzalez but i do read that we can add a custom URL pattern to web.xml file of the jetty library and like this we maybe could add a patter that will accept : inside the url. what do you think would that be possible?


 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Both links suggest to use /clients/* and it will catch client/323/book/6363.

What i mean and i think you mean is this one


As you can see jetty by design allows only the *

If you can catch this url client/323/book/6363
You can parse it manually, seems easy to do.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Both links suggest to use /clients/* and it will catch client/323/book/6363.

What i mean and i think you mean is this one


As you can see jetty by design allows only the *

If you can catch this url client/323/book/6363
You can parse it manually, seems easy to do.

ok, I understand so the only way is to parse the url.

thanx!

btw i was watching on the same page you posted above 😁
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
btw i was watching on the same page you posted above
I made the research months ago and arrived at that SO answer. At first i thought it was a deal breaker but after many projects it seems that you can live without that feature, it is not that important
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I made the research months ago and arrived at that SO answer. At first i thought it was a deal breaker but after many projects it seems that you can live without that feature, it is not that important

yes, you are right, regex will do the job :)
thank you,

btw, i can see from other posts that u r very familiar with web apps and servers. good to have here specialists like you :)
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Just made a topic on this here:
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
my question is can i do it without adding the value keys in the url?
You can achieve it using pretty URL:
For example: https://api.puterise.com:17179/user/view/3

The "key" elements are separated with a forward slash.

In B4J:
B4X:
Dim elements() As String = Regex.Split("/", req.RequestURI)

First, I have predefine the patent.
B4X:
ELEMENT_ROOT = 0
ELEMENT_CONTROLLER = 1
ELEMENT_ACTION = 2
ELEMENT_ID = 3

Then read the value of key by index.
B4X:
    Select elements(Main.ELEMENT_ACTION)
        Case "view"
            If elements.Length - 1 = Main.ELEMENT_ID Then
                Select elements(Main.ELEMENT_ID)
                    Case "all"
                        View("all")
                    Case Else
                        View(elements(Main.ELEMENT_ID))
                End Select
            Else
                Response.SendError(500, "Unknown action")
            End If
        Case Else
            Response.SendError(500, "Unknown action")
            Return
    End Select

The code snippets above are part of my API Server project.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I hope this template can help you solve this problem.
 
Upvote 0
Top