Thursday, April 3, 2008

JavaScript on the server with Helma

Rumor has it that the most widely deployed programming language is JavaScript, due to being embedded in every contemporary browser. This extraordinary feat has obscured the fact that the language itself is not only suitable for client programming, but for server-side development as well. Dynamic, loosely-typed languages offer shorter development cycles, agility in meeting changing customer requirements, particularly for enterprises without well-specified business processes, or rapidly expanding enterprises that constantly adapt their core business. JavaScript on the browser is a de-facto standard; using it on the server provides the benefit of reusing a single code base and data model, and debugging it once.

It is also relatively unknown that there exist already more than one frameworks for programming on the server with JavaScript. The most mature among them is Helma, an open-source framework that has been used in production environments for years, like the Austrian Broadcasting Corporation. I've been playing with it for quite some time and I'd like to show how easy it is to get up and running, in order to make any adventurous souls less afraid to give it a try.

Setting up Helma is fairly straightforward. First you have to download the latest version from http://helma.org/download/. Follow the proper link for your platform and the zip or tar archive will be downloaded in your system. After extracting the archive it will create a folder with the latest version of Helma (1.6.1 as of this writing) in it. The only prerequisite is that you have Java installed on your system, since Helma uses the Rhino JavaScript engine underneath. This arrangement allows the reuse of existing Java code through the Rhino APIs. If your system did not come with Java preinstalled, a visit to java.com should suffice. You should get the latest version available (Java 6 as of this writing), but anything after 1.4 should be good. If you are set, go to the Helma directory and execute start.bat or start.sh depending on your platform and you should see some brief startup messages:


Mac:~/helma-1.6.1 panagiotisastithas$ ./start.sh
Starting Helma in directory /Users/
panagiotisastithas/helma-1.6.1
Starting HTTP server on port 8080
Starting Helma 1.6.1 (January 8 2008) on Java 1.5.0_13


The server is now ready to accept requests. Helma embeds Jetty for its HTTP engine, something that makes the startup process a snap and provides for some interesting architectural choices with its support for continuations. Point your browser to http://localhost:8080/ and you should see the welcome page:



This is an actual application, the “welcome” application, that you are interacting with. You might want to navigate around a bit to skim through the available on-line documentation. When you've had enough, you can stop the server by getting back to the console window and hitting Control-C:


^CShutting down Helma - please stand by...
Mac:~/helma-1.6.1
panagiotisastithas$


Just to recap:
  1. Download Helma from http://helma.org/download/
  2. Unzip
  3. Go to the unzipped directory
  4. Make sure you have Java 1.4+ installed
  5. Execute start.bat/start.sh
  6. Point the browser to http://localhost:8080/

Creating a simple application with Helma involves only a few easy steps. First of all create a directory called 'hello' inside the subdirectory 'apps' of the main Helma installation directory. Inside 'hello' create a directory named 'Root'. Inside 'Root' create a file called actions.js with the following content:


function main_action() {
res.write("Hello, World!")
}


The following screenshot illustrates our progress so far:



Then open the file apps.properties in the main Helma installation directory and add a single line at the end of the file with the following content:


hello


Now start (or restart if it is already running) the server and point your browser to http://localhost:8080/hello. This is what you should see in your browser:



That's it! Your first server-side JavaScript application is ready. In future posts I intend to discuss how Helma wires the various pieces together and explore some of its more interesting features. In the meantime, I suggest that you download it and experiment. You might be pleasantly surprised!

6 comments:

synodinos said...

I feel strongly for JavaScript both on the client -side and as an all purpose scripting language. Also I believe that the next version will smoothen-out some rough edges.

There are several server-side JavaScript implementations that have either:

* endured the test of time like Cocoon, Rhino, mod_js, Resin

* are relatively new and seem promising like Phobos and Jaxer or

* are planned for the near future and for the time being are featured only in our wet dreams

The notion of server-side JavaScript is mature and tested. I believe that if it wasn’t for the acquisition of the Netscape Enterprise Server from Sun, which at that time hadn’t discovered dynamic languages, Limewire would have evolved into a diverse set of open source implementations based on JavaScript.

What pisses me of lately is all those people demonstrating the power and features of languages like Ruby in contrast to Java, and the beauty of running Ruby on the JVM with JRuby, when in fact I have all those things for years using JavaScript and the Rhino Engine.

past said...

I believe that if it wasn’t for the acquisition of the Netscape Enterprise Server from Sun, which at that time hadn’t discovered dynamic languages, Limewire would have evolved into a diverse set of open source implementations based on JavaScript.

I suppose you mean LiveWire, not Limewire :-)

synodinos said...

You can imagine what I was doing with my bandwidth at that time...

Anonymous said...

nice tutorial.

Bajal said...

Very much useful. Thanks

disa said...
This comment has been removed by a blog administrator.
Creative Commons License Unless otherwise expressly stated, all original material in this weblog is licensed under a Creative Commons Attribution 3.0 License.