on 10-14-2018 11:35 AM
I understand that everybody knows it and it's probably my personal disorder. Nevertheless: could anyone share a clue on what "$1" exactly mean in xs-app.json route definition? Something like this:
{
"source": "^/some_url/(.*)$",
"target": "$1",
"destination": "some_destination"
}
The syntax route source/target is all regular expression based. Therefore I'd suggest just searching/reading up on the general concept of regular expression (particularly in the context of JavaScript as that is what the app router is written in). For example:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Thomas,
Clear. Would be much helpful to add at least similar reference to support code examples in the documentation.
Hi Sergey
Basically $1 = (.*)$ for example /some_url/abc will make $1 = abc. A part of a pattern that can be enclosed in parentheses (...) is called a “capturing group” in regex.
Here are more examples and explanation from sap/approuter module:
https://www.npmjs.com/package/@sap/approuter#example-routes
For example, if you have a configuration with the following destination:
[ { "name" : "app-1", "url" : "http://localhost:3001"} ]
Here are some sample route configurations:
Route with a destination
and no target
{ "source": "^/app1/(.*)$", "destination": "app-1"}
Since there is no target
property for that route, no path rewriting will take place.
If we receive /app1/a/b as a path, then a request to http://localhost:3001/app1/a/b is sent.
The source path is appended to the destination URL.
Route with case-insensitive matching
{ "source": { "path": "^/app1/(.*)$", "matchCase": false }, "destination": "app-1"}
This example is much like the previous one,
but instead of accepting only paths starting with /app1/, we accept any variation of app1's case. </br>
That means if we receive /ApP1/a/B, then a request to http://localhost:3001/ApP1/a/B is sent. </br>
Note: The property matchCase
has to be of type boolean. It is optional and has a default value true
.
Route with a destination
and a target
{ "source": "^/app1/(.*)$", "target": "/before/$1/after", "destination": "app-1"}
Route with a service
, a target
and an endpoint
{ "source": "^/odata/v2/(.*)$", "target": "$1", "service": "com.sap.appbasic.country", "endpoint": "countryservice"}
When a request with path /app1/a/b is received, the path rewriting is done according to the rules in the target
property.
The request will be forwarded to http://localhost:3001/before/a/b/after.
Note: In regular expressions there is the term capturing group. If a part of a regular expression is surrounded with parenthesis, then what has been matched can be accessed using $ + the number of the group (starting from 1).
In the last example $1 is mapped to the (.*) part of the regular expression in the source
property.
Route with dynamic destination
and target
{ "source": "^/destination/([^/]+)/(.*)$", "target": "$2", "destination": "$1", "authenticationType": "xsuaa"}
If you have a another destination configured:
[ { "name" : "myDestination", "url" : "http://localhost:3002"} ]
when a request with the path /destination/myDestination/myTarget is received, the destination will be replaced with the url from “myDestination”, the target will get “myTarget” and the request will be redirected to http://localhost:3002/myTarget
Note: You can use a dynamic value (regex) or a static string for both destination and target values
Note: The approuter first looks for the destination name in the mainfest.yaml file, and if not found, looks for it in the destination service.
Hope it will help 🙂
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Konrad for the detailed explanation
Cheers
I am sorry for the late answer, I am not doing in SAP anymore. As I remember the soruce was README of sap/approuter package https://www.npmjs.com/package/@sap/approuter#example-routes
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.