mod_js – a JavaScript Apache module

mod_js is an Apache module written by Ash Berlin and I that embeds the SpiderMonkey JavaScript engine and lets you run JavaScript code on the server as CGI scripts. As you’d expect, it’s very early, but is good enough to compile scripts, run the JavaScript, and print the output to the client. mod_js is licensed under the GPL3.

You can get the source code from the subversion repository here, and there’s a trivial example JS script here. To build mod_js, make sure that the apxs binary for the apache to want is in your path (or set the APXS environment variable to the right binary) and run:

$ perl
$ sudo perl install

You’ll need root to install the module, and a working perl to run I don’t use perl for anything clever – I just prefer it to writing makefiles. Once the module is installed, edit your apache config file and add:

ScriptAlias /js-scripts /some/path/to/scripts
AddHandler js-script .jss

Once this is done, files in the script path ending in .jss will be run as server-side javascript files. See the example here for sample usage of server-side JavaScript.

Right now you can’t do a lot in a script – ‘print’ is about it. There’s a ‘request’ object available to the script that contains the CGI arguments, local script environment and any data that was posted to the script, so you can write simple CGI scripts, but there’s no access to the local filesystem or any database drivers (SQLite is linked in, and has very limited support, but it’s not useful yet). It’s possible that this situation will never get better – see the musing here for my reasoning.