I think you ARE missing quite a few things, yes:
1/you define services in config.json, which is a central place for external web services configuration (and note that config.json now accepts #include and #parse if you want to organize your solutions/services configuration in various - easy to manage - related files), also note that the config.json configuration is dynamic, meaning you can modify it while your server is running, nothing to redeploy, the changes will be picked up automatically upon the next call/request.
2/ the invocation of services can be synchronous or asynchronous, depending on whether you are doing
- Code: Select all
var result = plugins.Velocity.invokeService(serviceName, params);
or
- Code: Select all
plugins.Velocity.invokeService(serviceName, params, callbackFunction);
3/ when you invoke services programmatically, the services you invoke can be called from the client side or the server side, simply define a processType: "client" or processType: "server" - the plugin takes care of all the plumbing and uses a dedicated thread if it is called asynchronously (no need to span a headless client for example).
4/ not only can you call the services programmatically, but you can also ask Velocity to invoke them during a request or a response cycle, using processType: "pre" or processType: "post".
- With 'pre' the service will be invoked upon a request on a path/route that you define (route property in the service object), and the result of the invocation will be added to the request object (a plain JavaScript object) that you get in your vr_getContext() method, the result will be inserted in the request.parameters collection, with a property name that you can define (insertAs in config.json) or bearing the name of the service.
- With 'post', the service will be invoked during the construction of the response, still on a path/route you've defined, so the service will be called after you have returned a response in your vr_getContext() method, and its results will be added to the context of the response (with a property name defined by insertAs in config.json or the service name if no insertAs is given), then the added information can be used to fill a template (of any sort), or to return a JSON or XML response automatically.
5/ you will likely have to concatenate a string when you are calling your service:
- Code: Select all
var theURL = "http://data-mosaic.com";
var theSize = 'x';
mediaColumn = plugins.http.getMediaData("http://pagepeeker.com/thumbs.php?size="+theSize+"&url="+theURL);
personally, I find the code below more readable/maintanable:
- Code: Select all
var theURL = "http://data-mosaic.com";
var theSize = 'x';
mediaColumn = plugins.Velocity.invokeService('pageKeeper', {url: theURL, size: theSize};
Knowing as well that if you ever need to change the URL of the service or add some extra parameters, you can do so in the config.json without the need to redeploy.
I would think that's plenty of reasons why using Velocity to invoke external services makes more sense!