wxJavascript: a great find

October 5, 2007

I’ve just been installing and trying out wxJavascript, which is a very useful server-side javascript library that, for me, seems to solve the problem of running javascript server-side for the online word processor project (current code-name: “Remote Writer”).

Amongst other things, it has an Apache module (mod_wxjs)and support for sqlite, both quite important for this project.

The configuration wasn’t too bad, and consisted of following the quick start instructions on the website, as well as:

  1.  Experiencing a problem with adding the wxWidget modules in the Apache httpd.conf file, so what I ended up with was:
    # mod_wxjs server-side javascript stuff (see: http://www.wxjavascript.net/mod_wxjs/index.html)
    LoadFile d:/wxjs/bin/libapreq2.dll
    LoadModule apreq_module "d:/wxjs/bin/mod_apreq2.so"
    #LoadFile "d:/wxjs/bin/wxmsw28ud_core_vc_custom.dll"
    #LoadFile "d:/wxjs/bin/wxbase28ud_net_vc_custom.dll"
    #LoadFile "d:/wxjs/bin/wxmsw28ud_adv_vc_custom.dll"
    LoadModule wxjs_module "d:/wxjs/bin/mod_wxjs.dll"
    AddHandler wxjs .wxjs
    wxJS_Modules "d:/wxjs/bin/modules.xml"
    wxJS_RtSize 1M
    wxJS_CtxSize 8K

    Which is not too bad I think, as for the work I want to do, hopefully I won’t need the wxWidgets.

  2.  I had to play around with the modules.xml file, reordering the entry for wxJS_gui.dll to the bottom of the list, as otherwise, when Apache tried to load these modules, it produced an error message
    Error: Failed to load shared library 'd:\\wxjs\\modules\\wxJS_gui.dll'
    (error 126: the specified module could not be found.)
    Error: wxJS: Module gui(d:\\wxjs\\modules\\wxJS_gui.dll )not loaded

    It then apparently refused to load the following modules, which I would detect in the log after trying to run a script using the sqlite module:

    Error: wxJS: D:\\Program Files\\...\\maori-bibliography\\dbtest.wxjs(3) :
    ReferenceError: sqlite is not defined

I then wanted to run the following test script to check the Sqlite connection:

var exists = false;
var created = false;
var handle = new sqlite.Database("htdocs\\gears\\editor\\tinymce\\maori-bibliography\\store.db");
if ( handle.opened )
    var pragmaStmt = handle.prepare("PRAGMA user_version");
    var pragma = pragmaStmt.fetchArray();
    if ( pragma[0] == 0 )
        // Create the tables and insert some example data
        handle.exec("CREATE TABLE authors(id INTEGER PRIMARY KEY, firstname TEXT, lastname INTEGER)");
        handle.exec("CREATE TABLE books(id INTEGER PRIMARY KEY, title TEXT, fk_author_id INTEGER)");
        handle.exec("PRAGMA user_version = 1");
        handle.exec("INSERT INTO authors(id, firstname, lastname) VALUES(1, 'JRR', 'Tolkien')");
        handle.exec("INSERT INTO authors(id, firstname, lastname) VALUES(2, 'John', 'Grisham')");
        handle.exec("INSERT INTO books(id, title, author) VALUES(1, 'Lords of the ring', 1)");
        handle.exec("INSERT INTO books(id, title, author) VALUES(2, 'The firm', 2)");
        created = true;
    } else {
        exists = true;
        handle = null;
response.print("<html><head><title>wxJS database test program</title></head><body>");
response.print("<b>Was the database already present?:" + exists + "</b><br/>");
response.print("<b>Were the tables created?:" + created + "</b><br/>");

However, running this test script wasn’t without some apparent wrinkles:

  1. Once I got this one worked out, I ran the script to create a database, which seemed to work fine, but the created Sqlite database didn’t appear in the home directory of the script, where I would have thought it would appear.
    It turns out that mod_wxjs seems to default to creating Sqlite databases in the Apache2.2 directory.
    So, in my script I had to specify the subpath to the location where I wanted the database created.

    var handle = new sqlite.Database("htdocs\\gears\\editor\\tinymce\\maori-bibliography\\store.db");
  2. The created database was not accessible via the command-line Sqlite3.exe tool, at least not until I’d shut down Apache (fair enough I suppose, as it created journalled entries in an associated file which were not applied until Apache shutdown).
  3. It seemed to ignore the Pragma identifying whether the database had been created, being quite happy to recreate the database. This errant behaviour stopped after I restarted Apache (i.e. after it had written out the journalled entries)

However, to date it seems to be usabl, and a great solution at that!


2 Responses to “wxJavascript: a great find”

  1. Franky Braem Says:

    You can use the global ‘root’ property to get the path of the script that is executed on the request.

    The database is not accessible because it is not closed yet. This is a bug in wxJavaScript: the JavaScript gc is not called correctly.

    Thanks for writing down your experiences with wxJavaScript. If you have any questions, you can ask them in the wxJavaScript newsgroup or on the project website.


  2. Somehow i missed the point. Probably lost in translation 🙂 Anyway … nice blog to visit.

    cheers, Microbiologically

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: