Occasionally I search for some JavaScript help and I come upon the term "Server-side JavaScript". When would you use JavaScript server-side? And how?
My experiences of JavaScript have been in the browser. Is there a compiled version of JS?
Occasionally I search for some JavaScript help and I come upon the term "Server-side JavaScript". When would you use JavaScript server-side? And how?
My experiences of JavaScript have been in the browser. Is there a compiled version of JS?
It could refer to using javascript to post messages to a web server without re-loading the page: in other words, AJAX.
But more likely I think it means something like Aptana/Jaxer, which uses javascript for a server-side language. In this case, remember that javascript is just a language: the DOM used in a web browser is a sort of API. The server-side javascript engines would provide their own API objects, geared at server-side tasks like DB and file system access.
Server-side javascript is an interesting idea because of the client-side validation problem: you want to do validation client-side to avoid posting needless queries to your server, but you must do validation server-side because you can't trust the client. This results in a lot of duplicate code between the client and server.
The theory is that if your client and server languages match you'll no longer need two implementations of the same logic. In practice it doesn't work so well, because the client and server views of a page request are so different and because you don't control the javascript engine used by the client.
Wikipedia has an entry but it tells me very little.
Torino looks interesting : "Server-side JavaScript environment for developing web applications. Provides a rich server-side programming environment using Java APIs. Released under the GPL3 license."
This article claims that SSJS is compiled into byte-code.
You might want to have some functionality both in the browser and in the server to have the exact same implementation.
An example would be a renderer for a wiki-syntax, that you run in the browser for the WYSIWYG editor and on the server to render the resulting page. This way you know that both the rendered results will be exactly the same in both cases.
Apparently Rhino can compile JavaScript to Java classes.
Traditionally, Javascript runs around the Document Object Model. But what if you work for a Java shop and would like a scripting engine around your custom object model? That's when Server-side Javascript comes in.
It really depends if you are talking about ASP.NET or Classic ASP. If you are using ASP.NET there aren't many good reasons for using Javascript.
ASP Classic is an different case. You can use Javascript on the server side in ASP just the same way you would use VBScript. You can access the Application, Server, Request and Response objects just the same as via VBScript.
There can be real benefits to using Javascript on the server side in ASP rather than VBScript. It means you can share code between the browser code and server code. It also means your developers don't need to deal with two different languages.
There are some downsides to server side Javascript in ASP though. Firstly it doesn't appear to be as fast as VBScript on the server side at string concatenation. It also isn't as good as making calls to COM objects as VBScript (you can only get data back from COM calls via the return value, rather than via out/byref parameters).
It's not AJAX, unless people are using the term improperly. As its name suggests, SSJS is JavaScript that runs on the server, interpreted by a standalone (i.e., browser-independent) JavaScript engine, like SpiderMonkey.
Why bother? Well, one area I currently see it underutilized in is in data validation. With SSJS you write one piece of code that then gets used on both the server and the client. Thus you get immediate user feedback from the client-side JS that will automatically match the data checking taking place on the server.
Classic ASP was able to use JavaScript on the server, although most people used VBScript.
One compelling use of JavaScript on the server is as a complement to client-side data validation. For example, you might use the same JavaScript validation library on the client and on the server. This ensures you're using the same logic on the client as you are on the server, but (potentially) avoiding an unnecessary round-trip by pre-validating on the client side.
Wikipedia lists a number of server-side JavaScript implementations here.
I remember with Cocoon (Apache's Java/XML/Javascript MVC framework) I used to use server-side Javascript since there was a something (I believe cforms) that needed to be written in Javascript and was running on the server even though I believe you could write it in Java.
We used Rhyno by that time, please check: http://peter.michaux.ca/articles/server-side-javascript-with-rhino-and-jetty
Yeah I've just read up about SSJS on a blog by some guy named John Resig.
He describes an engine called Jaxer, which he says is "Imagine ripping off the visual rendering part of Firefox and replacing it with a hook to Apache instead - roughly speaking that's what Jaxer is."
For anyone who knows ASP.NET The HTML looks familiar
<html>
<head>
<script src="http://code.jquery.com/jquery.js" runat="both"></script>
<script>
jQuery(function($){
$("form").submit(function(){
save( $("textarea").val() );
return false;
});
});
</script>
<script runat="server">
function save( text ){
Jaxer.File.write("tmp.txt", text);
}
save.proxy = true;
function load(){
$("textarea").val(
Jaxer.File.exists("tmp.txt") ? Jaxer.File.read("tmp.txt") : "");
}
</script>
</head>
<body onserverload="load()">
<form action="" method="post">
<textarea></textarea>
<input type="submit"/>
</form>
</body>
</html>
Note the runat="sever" and runat="both"
http://steve-yegge.blogspot.com/2007/06/rhino-on-rails.html
Check out how Steve Yegge is using Server-Side JavaScript with Rhino and why. He has a bunch of stuff on how he feels JavaScript is up and coming.
There's the project Phobos, which is a server side Javascript framework.
Back In The Day, the Netscape web server offered server-side javascript as well.
In both of these cases, Javascript is used just like you'd use any language on the server. Typically to handle HTTP requests and generate content.
Rhino, which is Mozillas Javascript system for Java, compiles Javascript in to Java byte codes, which the JVM can choose to JIT. Other systems use other means for executing javascript, even to the point that some are JIT compiling their javascript internal codes.
I forsee that there will be more and more Javascript on the server. When you're writing "thick" applications in Javascript on the client, then you may as well be able to write your logic in Javascript on the server in order to not have to make the cognitive leaps from one language to another. The environments will be different, but much of your code and knowledge will be sharable.
Finally, Javascript is probably the singular language that has the most money pointing at it right now in terms of implementations. From Apple, Mozilla, Google, and even Microsoft as well as the efforts to make it an even more advanced language (i.e. basically a Scheme with Algol syntax sans macros).
Most of those implementation are buried in the browser, but that's not to say that there's no value on the server side as well.
The tooling is the biggest place where Javascript is lacking, especially on the server side, but if you consider something like Phobos, where you can debug your server side Javascript in the IDE, that's a great advancement.
Personally, I'm tossing Javascript around in my applications like white paint. It offers cheap extensibility for very little cost and is a great enabler.
AppJet offers free server-side Javascript applications. You can create as many applications as you wish and there are some nifty libraries built in for grokking web pages, outputting HTML, persistent object storage, array handling, etc.…
You're limited to 10M of storage but there is also cron capability via library calls and the ability to create libraries…
You can read my take on appjet on my programming tumblr log
With ASP you can use Server Side JavaScript in a number of ways. The way I most commonly use it is to have the same code executing on the client and on the server for validation.
file.js
<!--//--><%
//javascript code
function example(){return "Hello, World!";}
//%>
file.html
<%@LANGUAGE="javascript"%>
<!-- METADATA TYPE="typelib"
FILE="C:\Archivos de programa\Archivos comunes\System\ado\msado15.dll" -->
<!--#include file="file.js"-->
<html>
<head>
<script language="javascript" src="file.js"></script>
</head>
<body>
<%=example();%>
<script language="javascript">alert(example());</script>
</body>
</html>
file.js
starts and ends the way it does to allow for inclusion of the same file.