Spidermonkey CGI hello.js
There is nothing quite like the thrill of successfully running a new "hello, world" program. I'd like to think it's the anticipation that something new and great can be created after. Something like a big door being unlocked. However, I think it is really just relief that the damn software is finally installed, working, and I can finally get on with things.
With all the recent thinking about JavaScript on the server, today I wrote "hello, world" as a server-side script using Apache cgi and Spidermonkey on my Mac. I think other front-end developers will want to try leveraging their JavaScript knowledge on the server-side even if it just to see JavaScript run somewhere else for a change. These instructions might help someone get his "hello, world" fix.
Installing Spidermonkey
Spidermonkey is the Mozilla C implementation of JavaScript. We can download and install Spidermonkey as a regular scripting language to use at the terminal prompt just like Perl, Python, or Ruby.
$ mkdir ~/Desktop/src
$ cd ~/Desktop/src
$ curl -O http://ftp.mozilla.org/pub/mozilla.org/js/js-1.60.tar.gz
$ tar xzvf js-1.60.tar.gz
$ cd js
$ make -f Makefile.ref
Now you can give the interactive shell a try. The binary is in a subdirectory and we can try it from there for now.
$ cd Darwin_DBG.OBJ
$ ./js
js> print('hello, world');
hello, world
js> help();
JavaScript-C 1.6 2006-11-19
Command Usage Description
======= ===== ===========
version version([number]) Get or set JavaScript version number
...
js> quit();
If you are accustomed to browser JavaScript you won't recognize print(), help() and quit() as properties of the global window object. These functions are properties of the global object in the Spidermonkey shell. We could just as easily write this.print('hello, world');
It would be much better to move the JavaScript binary to a location in our PATH. On my computer I use /usr/local/bin/ which I can see is in my PATH.
$ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/pgsql/bin
Move the js binary
$ sudo mv ~/Desktop/src/Darwin_DBG.OBJ/js /usr/local/bin
Now it is a couple characters easier to start the js shell.
$ js
js> quit();
You can also write a script file and run the script. I use Textmate to write code.
$ cd ~/Desktop
$ mate hello.js
The contents of the file is just a single line
print('hello, world');
After you save the file try this. (The -f seems to be optional.)
$ js -f ~/Desktop/hello.js
hello, world
Spidermonkey allows for a shebang line. Edit the hello.js file to be something like this (depending on the location of your js binary.)
#!/usr/local/bin/js
print('hello, world');
Now we can try to run the file with ./hello.js which fails until the file permissions are set to executable.
$ cd ~/Desktop/
$ ./hello.js
-bash: ./hello.js: Permission denied
$ ls -l | grep hello.js
-rw-r--r-- 1 peter peter 42 Jul 7 16:23 hello.js
$ chmod 755 hello.js
$ ls -l | grep hello.js
-rwxr-xr-x 1 peter peter 42 Jul 7 16:23 hello.js*
$ ./hello.js
hello, world
That's enough with Spidermonkey.
Installing Apache 2.2
You don't need Apache version 2.2 specifically but here is one way to install it. If you have another version of Apache running turn if off. Perhaps in the Apple Menu|System Preferences|Sharing pane.
I installed Apache 2.2 in /apache2/, of all places, but is should be obvious how to change the location.
$ cd ~/Desktop
$ curl -O http://apache.oss-mirror.org/httpd/httpd-2.2.4.tar.gz
$ gnutar -xzf httpd-2.2.4.tar.gz
$ cd httpd-2.2.4
$ sudo ./configure --prefix=/apache2 --enable-module=most --enable-shared=max
$ sudo make
$ sudo make install
$ sudo /apache2/bin/apachectl start
Point your bowser to
http://localhost/
and you should see the "It works!" page.
For fun, edit the "It works!" file. Just add some more exclamation marks or something.
/apache2/htdocs/index.html
reload in browser
http://localhost/
and you should see your changes.
You can stop Apache for now.
$ sudo /apache2/bin/apachectl stop
Configuring Apache
Make a backup of your Apache configuration file before you play!
$ cd /apache2/conf/
$ sudo cp httpd.conf httpd-20070707.conf
$ sudo mate httpd.conf
I was able to trim my Apache config file down to just the following. I'm sure it is not efficient or secure but I wanted to see how small I could get the big intimidating config file.
ServerRoot /apache2
PidFile /var/run/apache2.pid
User peter
ErrorLog /Users/peter/Desktop/error.log
Listen 80
DocumentRoot /Users/peter/Desktop/www/
ScriptAlias /cgi-bin/ /Users/peter/Desktop/www/cgi-bin/
Now we create the cgi JavaScript
$ makedir ~/Desktop/www/
$ makedir ~/Desktop/www/cgi-bin/
$ mate ~/Desktop/www/cgi-bin/hello.js
The contents are just three lines.
#!/usr/local/bin/js
print("Content-type: text/html\n\n");
print('hello, world!');
Set the permissions on the script
$ chmod 755 ~/Desktop/www/cgi-bin/hello.js
Start apache
$ sudo /apache2/bin/apachectl start
And see the glorious page in the browser at the following address.
http://localhost/cgi-bin/hello.js
Running JavaScript as a cgi script is sort of like how people used Perl before the days of mod_perl. It is old fashioned but it still has the "hello, world" thrill.
Comments
Have something to write? Comment on this article.
Have something to write? Comment on this article.
feed
I keep meaning to get more into using JS on the server side. It's a very under rated language.