Alcides Fonseca

40.197958, -8.408312

JSRocks (Server-side JavaScript with Python)

John Resig posted about the State of The Art in terms of embedding javascript in other languages. I was glad to see that there was one Python related project: Python-SpiderMonkey. Then a nice hack idea popped in my head: now I can do JavaScript Server-Side like I want!

So if you want, you can download my Javascript implementation of a simple CGI script in Python where you can access GET, POST, SERVER and COOKIES variables through request.get, request.post, request.server and request.cookies (also have request.post_files, but I haven’t tested uploading files). This is based on my previous work in pungi. And you also have a new File class (with read, write, readlines and close methods) as well as Sqlite (a wrapper for SQLite3 databases, with only the query method). This is the basics that allow you to write your first webapplication based on Javascript.

If you wish, you can download it and test it yourself. I guarantee nothing!

  • Install Pyrex (sudo easy_install pyrex)
  • Install Python-SpiderMonkey ( SVN checkout and sudo python setup.py install
  • Copy the jsrocks folder to your htdocs. Keep in mind you need mod_rewrite and .htaccess files enabled.
  • Just visit /jsrocks/ and /jsrocks/test (which call index.js and test.js)

NOTE! This is just a hack to prove it could be possible. Using an interpreted script to call another interpreter is not the best idea. Oh, and through CGI even less plausible (fastCGI is more admissible though). I believe that something like mod_js is a better approach (although last time I checked it lacked File IO and Database connections). Google (Steve Yegge) apparently likes the idea of using Rhino, which I don’t like that much.