Server-Side Javascript


(Squiz) #1

Stumbled on the Javascript REST Resource the other day. It’s a pretty cool asset that when configured correctly lets you run server-side Javascript to manipulate the response from a HTTP request to another system.


We’ve got a lot of systems in our business that we can expose via SOAP web service, so it’s useful to be able to perform queries, calculations, and put some logic in the responses we get back. For instance I’ve just finished a neat intranet tool to get data from our order entry system and perform calculations using the very latest figures.



Some may consider server-side Javascript an awkward idea, but it’s picking up a bit of momentum with projects like Javascript REST Resource. Admittedly the Matrix implementation isn’t that fancy, but it’s still quite useful in some scenarios.



Checking for Requirements

To set this up you will need to install a third party Javascript engine. Your operating system probably won’t come with one installed by default, so attempting to run any Javascript with this asset will just fail silently.



You can tell if the Javscript engine is installed on your server by checking the Matrix configuration: “System Configuration” → “External Tools Configuration” → “JavaScript Configuration” → “Javascript Command”. This should default to something like “/usr/bin/js”.



If the command is present, you should be able to run it on your server and get the Javascript console.


    # js
    js>

Installing Javascript

There’s a few different Javascript engines, but I’m using Javascript REST Resource from Mozilla. You may also consider using Rhino or V8.



If you’re using Red Hat or Fedora, check to see if your distribution has a js and js-devel package in the repository. If not, you can find one on Javascript REST Resource.



Using Javascript

Dealing with Javascript on the server is a bit different to using it in the context of the browser. The main thing you will probably notice is that there’s no global ‘document’ object, and a number of the more helpful libraries from modern Javascript are missing.



Regardless it still quite a powerful when you work out what you’re doing. Here’s a few tips to get you started:



  • Safely load and save JSON by including a Javascript REST Resource.
    [*] Create XML documents with the following syntax:
        var newDocument = new XML('');

    [*]If you have a requirement for a Javascript templating language, consider using Javascript REST Resource.

(Squiz) #2

As yet I haven't found a way to get access to any inputs to the REST Resource Javascript asset. Keyword replacements behave strangely, so I thought I'd document it here in case anyone else runs into the same issue.


Keyword Replacements (Don't Work)

When you put a keyword into the JavaScript field, it actually processes it after the Javascript has returned. So for instance, the following code would seem to work, but now how you think:


    print("%globals_get_mode^eq:up:up:down%")

The code would print "up" or "down" depending on the GET parameter on the end of the web path. HOWEVER, if you were to try the following code, it would return "32":


    print("%globals_get_mode^eq:up:up:down%".length)

The reason for this is that the keyword is actually being parsed by Matrix after the Javascript has executed and returned the output. This means that to the JS interpreter the string is always actually "%globals_get_mode^eq:up:up:down%" rather than the expected "up" or "down".



Passing Parameters to the JS Interpreter

There's a couple of ways I can think of to pass these parameters to the Javascript, but they both involve sending the data to the server first.



[list=1]

  • Send the parameters to the server, and get the server to echo them back.
    [*]Send the parameters to the server via the "URL(s)" or "Request Headers" fields, but read them back from the _REST object.

    You could read the headers from the [i]_REST.request.headers[/i] field, or the [i]_REST.request.urls[/i] field.

  • (Anton Babushkin) #3

    Hi Ash,


    Thanks for sharing this with the community.



    I've actually been using the REST asset quite often as well recently and it's great for a lot of the "integration" style projects.



    Another useful case for it, is to do some simple post-processing. For example, you may want to do additional processing over an asset listing / search page asset without relying on client-side JS (i.e. progressive enhancement might matter to you).