Server-side JavaScript under Apache

Posted 09 July 2007 in Blog tagged with [apache] [javascript] [programming] [spidermonkey] (edit)

Ash Berlin and I recently hacked together an Apache module that embeds the SpiderMonkey JavaScript engine and lets you run JavaScript code on the server as CGI scripts. We called it mod_js.

It was written as a proof-of-concept module at the last Fotango hack-day - Ash and I used my ancient [mod_tt](http://jerakeen.org/code/mod_tt/) as a starting point and had a module capable of printing to the client in about 4 hours of work. Not bad. See the mod_js project page for the source code, examples and build instructions, if you want to have a look yourself.

Right now in mod_js you can print output to the client, and access CGI parameters in the incoming request. There’s no filesystem access or database access. This leads me to a dilemma. I feel there’s use in a stand-alone server-side JavaScript programming environment, but heavier ones than this exist already. Faced with having to re-implement the entirety of the Perl DBI layer, TCP/IP networking, disk access, etc, etc, with all the associated security problems that will happen, I’m forced to consider mod_js somewhat of a dead-end. It would probably be much more useful implemented as a mod_perl module using the Perl SpiderMonkey bindings to embed JavaScript, so I could use all the existing Perl infrastructure to support my scripts. It would be heavier, but there will be more hosting companies offering mod_perl than are willing to compile new Apache modules, or it could be run as a stand-alone daemon more easily than something written in C.

Given all of this, mod_js is still a nice proof-of-concept, though, and could be turned into a real server-side programming language. I also quite enjoy working in C every so often. It reminds me why I don’t do it more.

Update: Ash gave it a real web page. Crazy.

Harry Fuecks

2007-07-09 22:57 on Server-side JavaScript under Apache in Blog

You could try teaming up with this guy - http://www.wxjavascript.net/mod_wxjs/index.html

 

2007-07-09 23:14 on Server-side JavaScript under Apache in Blog

Haxe has a pretty interesting approach: http://www.haxe.org/

Lawrence Oluyede

2007-07-09 23:16 on Server-side JavaScript under Apache in Blog

Why not embedding Rhino instead of SpiderMonkey? Then you have all Java at your command

Tom Insam

2007-07-10 07:58 on Server-side JavaScript under Apache in Blog

Well, the _real_ reason we didn't is because we had 5 hours to get a proof-of-concept, and SpiderMonkey is a tiny little C library. Embedding the entire JVM woul be.. interesting. Also, (http://mod-gcj.sourceforge.net/) it's been done...

There's not really any shortage of server-side JavaScript environments out there, and I'm not really trying to present mod_js as something _serious_. It's a demonstration that this stuff really is quite easy.

other pages