Server side Javascript
[ An update to the code demonstrating E4X support is here ]
Ajaxian is reporting that Sun is releasing Phobos, a Javascript application server. This is a web server that runs Javascript for implementing the server side code. The source has not yet been released, according to Ajaxian, but will be.
A while back I worked on doing server side Javascripting using Rhino as the interpreter and Jetty 6 as the server. I got it to work but never tidied it up for release. Sun's Phobos has motivated me to make it available so I've tidied it up and it can be downloaded from javascript-server.tar.gz. A darcs repository with the code is here:
darcs get http://www.bluishcoder.co.nz/repos/javascript-server
The repository contains the Jetty 6 JAR files and the Rhino interpreter JAR, along with an example Javascript file showing how it works and a readme.
Once the 'example.js' is loaded into the Rhino interpreter you can start a Jetty 6 web server on port 8080 with the following command:
A simple 'HelloWorld' style servlet looks like this in Javascript:
It has a 'ProcessGet' function which is called when an HTTP GET is made. The 'req' and 'resp' objects and the standard Java HttpServletRequest and HttpServletResponse respectively.
The magic of deriving from HttpServlet is done by Rhino. But it can't actually override HttpServlet methods - it can only override abstract methods. To fix this I created a JavascriptServlet Java class which forwards doGet(...) to an abstract 'ProcessGet', which is the method you see overridden above.
With the Jetty server we started running from the interpreter earlier we can add this servlet dynamically:
Now requests to http://localhost:8080/ will run HelloWorldServlet. When can dynamically add other servlets too:
Requests to http://localhost:8080/bye/ will run the GoodbyeWorldServlet.
Using the Jetty API you can work out how to add, remove and otherwise do some very cool stuff. Like play with Jetty 6's Ajax continuation support.
The server can be stopped with:
Running Javascript on the client and server gives some interesting possibilities. Sharing code for example. Or writing validation rules in Javascript which run both on the client and server.
The Dojo javascript toolkit can run client side or server side. This means you could use Dojo's packaging and other nice features for server side development in Javascript.
Rhino has continuation's which are serialisable. A continuation based web server could be implemented which serialises continuations like SISCWeb.
Download javascript-server, play with it, and let me know what you do with it. I hope to follow up on some of the above ideas too.
Categories: javascript, web
Ajaxian is reporting that Sun is releasing Phobos, a Javascript application server. This is a web server that runs Javascript for implementing the server side code. The source has not yet been released, according to Ajaxian, but will be.
A while back I worked on doing server side Javascripting using Rhino as the interpreter and Jetty 6 as the server. I got it to work but never tidied it up for release. Sun's Phobos has motivated me to make it available so I've tidied it up and it can be downloaded from javascript-server.tar.gz. A darcs repository with the code is here:
darcs get http://www.bluishcoder.co.nz/repos/javascript-server
The repository contains the Jetty 6 JAR files and the Rhino interpreter JAR, along with an example Javascript file showing how it works and a readme.
Once the 'example.js' is loaded into the Rhino interpreter you can start a Jetty 6 web server on port 8080 with the following command:
var s = startServer(8080);
A simple 'HelloWorld' style servlet looks like this in Javascript:
HelloWorldServlet = makeServlet({
ProcessGet: function (req, resp) {
var text = "Hello World!";
resp.setContentType("text/plain")
resp.setContentLength(text.length)
resp.getOutputStream().print(text)
resp.flushBuffer()
}
});It has a 'ProcessGet' function which is called when an HTTP GET is made. The 'req' and 'resp' objects and the standard Java HttpServletRequest and HttpServletResponse respectively.
The magic of deriving from HttpServlet is done by Rhino. But it can't actually override HttpServlet methods - it can only override abstract methods. To fix this I created a JavascriptServlet Java class which forwards doGet(...) to an abstract 'ProcessGet', which is the method you see overridden above.
With the Jetty server we started running from the interpreter earlier we can add this servlet dynamically:
addServlet(s, "/", HelloWorldServlet);
Now requests to http://localhost:8080/ will run HelloWorldServlet. When can dynamically add other servlets too:
addServlet(s, "/bye", GoodbyeWorldServlet);
Requests to http://localhost:8080/bye/ will run the GoodbyeWorldServlet.
Using the Jetty API you can work out how to add, remove and otherwise do some very cool stuff. Like play with Jetty 6's Ajax continuation support.
The server can be stopped with:
s.stop();
Running Javascript on the client and server gives some interesting possibilities. Sharing code for example. Or writing validation rules in Javascript which run both on the client and server.
The Dojo javascript toolkit can run client side or server side. This means you could use Dojo's packaging and other nice features for server side development in Javascript.
Rhino has continuation's which are serialisable. A continuation based web server could be implemented which serialises continuations like SISCWeb.
Download javascript-server, play with it, and let me know what you do with it. I hope to follow up on some of the above ideas too.
Categories: javascript, web

10 Comments:
I don't see why someone should use JavaScript as a server side language of choice. I get sick of of client side JavaScript coding not to want to welcome it in the backend.
It's kinda sad that negative JavaScript experiences in the browsers still spill over; on the server side it's a much more predictable affair (and indeed a very nice language to develop in).
We've been using Helma (probably the JavaScript application server - it's in production use on sites that have millions of regular visitors) for several years and are (still!) very happy with it.
I agree, Javascript strikes me as a great server side language. Thanks for the pointer to Helma, I'll take a look at it.
JavaScript is a very sexy language, not appreciated enough. I have been looking for a nice open-source implementation.
Whoever says JavaScript is lame does not know what his talking about.
wxJS (http://wxjs.sourceforge.net) has an Apache module mod_wxjs which makes it possible to use JavaScript (with E4X and wxWidgets ported classes) for generating dynamic webpages.
I was looking for exactly this solution and was even looking to pay for it. Thanks!
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21954496.html
http://answers.google.com/answers/threadview?id=724927#a
http://groups.google.com/group/mozilla.dev.tech.js-engine/browse_frm/thread/fa524df85495854b
I think the holder object in Jetty 6 was a big part of the solution, since that didn't exist when I was first looking at this problem.
Great, glad it proved useful!
I think ecmascript is probably the most powerfull language used in the mainstream. To bad people don't like it, or maybe they just don't understand it..
I think that coding the same language both server and client side is a very seductive idea. You can even communicate using the same language ! (JSON)
Anyway, I have to give it a try :p
Chris,
Any chance you can reduce the footprint and add a well-qualified web proxy? :-)
There is potentially
some good work going on around the Dojo toolkit and it would be great if they could write the server/proxy-side code in JavaScript to keep the code all in one language. I think they need a JavaScript interpreter to handle the "PAC" file, but I haven't convinced them of that yet.
Thanks for this example, Chris. I've massaged example a bit to make it more Rhino on Rails like. my blog post about it.
Post a Comment
<< Home