views:

1343

answers:

13

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?

A: 

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.

Joel Coehoorn
A: 

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.

John Nolan
+1  A: 

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.

Francisco Canedo
A: 

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.

http://en.wikipedia.org/wiki/Server-side_JavaScript

yogman
+2  A: 

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).

andynormancx
The OP never mentioned a specific technology; he could just as easily be thinking of something like Jaxer.
Adam Lassek
Lots of people don't realise that you can even do server side Javascript in Classic ASP. So I thought it would be helpful explaining that the term "Server-side Javascript" could just be referring to plain old ASP Classic.
andynormancx
JScript string concat is slightly slower using the operator (+), but pushing onto an array and joining is very fast and not much more difficult. If you are concat'ing enough strings to matter then it is no harder to do: var buffer = []; buffer.push('strings'); return buffer.join('');
Prestaul
+13  A: 

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.

Kev
I like this idea +1
Michael Haren
Some day I'd like to automatically generate JavaScript from database CHECK constraints this way. (I wonder if pgsql has JS bindings?)
Kev
+1, never thought about the validation angle
orip
I'm using this approach on some legacy ASP apps. It's not without issues (the same issues you'd face with IE vs FF vs Opera), but once you have managed to make it work, it works great.
voyager
+12  A: 

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.

Lee Harold
I prefer your wording to mine. :) +1
Kev
We used JScript w/ ASP at my last company. The bonus is fewer languages (we had front-end developers writing some server-side data calls) and code re-use, because you'd use almost exactly the same code to validate on both sides. Good stuff, but now I'm struggling to find good ways to put it into Apache.
Alex Mcp
A: 

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

igorgue
A: 

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"

John Nolan
I posted this above, but maybe it belonged here... Some feedback on Jaxer: http://stackoverflow.com/questions/98915/is-anyone-familiar-with-jaxer-im-looking-for-pros-and-cons
Prestaul
A: 

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.

Greg
I don't have the spare 6 hours for a Yegge post.
John Nolan
Man, I wish I could up-vote a comment. :)
Christian Nunciato
+4  A: 

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.

Will Hartung
oberhamsi
A: 

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

Naum
A: 

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.

voyager