Backend URL parameters for your API made with QCObjects Built-In Server
I'm happy to announce that since the version 0.1.45 of QCObjects CLI Tool you can use parameters inside the URL and catch them in a regular expression passing a converted object to the micro-service package:
Here is an explanation:
When you put the backend path into the config.json (server side) file like this:
"backend": {
"routes": [
{
"path": "^/store/(?<store>.*)/product/(?<product>.*)/(?<subproduct>.*)/.*$",
"microservice": "org.quickcorp.custom.backend.api.store.products"
}
]
}
An object will be created with the properties store, product and subproduct, and assigned to the micro-service package Class instance org.quickcorp.custom.backend.api.store.products.Microservice as a routeParams property.
So, inside your micro-service you can define a get method to use the params like this:
get:function (data){
let microservice = this;
microservice.body = {
result: 'Hello World!',
routeParams: microservice.routeParams
};
microservice.done();
}
Then, if you visit your micro-service using this example URL:
> curl -k https://localhost/store/1/product/1234/3454/
The expected response of the micro-service would be like this:
{
"result": "Hello World!",
"routeParams": {
"store": "1",
"product": "1234",
"subproduct": "3454"
}
}
In other words, you can use this.routeParams.store from a URL that is matched by a route that defines a (?<store>.*) group into its regular expression. Cool and nice isn't it?
Any regular expression group will be matched and mapped to the routeParams property of your micro-service instance.
One more power to this powerful tool!
To upgrade/update or install to the latest version of QCObjects CLI Tool, execute the following command:
> npm cache verify && npm i -g qcobjects-cli
If you want to learn more about QCObjects, go to the main reference