5

I talked to the Team Lead at Snap-On Tools once, and she told they used an "implementation of JavaScript" for their server-side coding. It's been a while, but I was thinking, WTF is she talking about? Are there interpreters for JavaScript besides those implemented in browsers?

How can you create a program or code, especially server-side, using JavaScript that doesn't execute in the context of a browser? What the hell is server-side about JavaScript if it's not generating content after the browser has loaded it? Can "server-side" JavaScript generate content before the HTTP response is deliver—and if so, how does that work/is set up?

I have many issues with JavaScript, but first-class functions are so sexy. And JavaScript Object Notation is so pure; I couldn't imagine an easier way to define data structures. Plus, you can hack out some code pretty quickly with dynamic typing if you're not writing something that's mission critical.

As a side question, given the last paragraph, have any suggestions about a good language to learn (comments will suffice)?

core
  • 30,054
  • 41
  • 131
  • 189
  • You probably mean "SpiderMonkey" (with a 'k'). :) – Hosam Aly Jan 25 '09 at 07:29
  • Oops, typo by omission! Thanks Hosam. Bonus points for 'k' instead of "k"! :) – core Jan 25 '09 at 07:56
  • I asked a similar question [http://stackoverflow.com/questions/459238/when-and-how-do-you-use-server-side-javascript](http://stackoverflow.com/questions/459238/when-and-how-do-you-use-server-side-javascript) – Johnno Nolan Jan 25 '09 at 07:58

12 Answers12

14

JavaScript does not have to be run in a browser if you use an ECMAScript engine. Actually, both SpiderMonkey and Rhino are ECMAScript engines.

Flash's ActionScript is another ECMAScript derived language that doesn't have to run in a browser.

Edit - Wow, a lot has changed in three years. For your server-side needs, I now recommend node.js.

DavGarcia
  • 17,702
  • 14
  • 52
  • 94
5

List of JS interpreters that I know of, that can run standalone or embedded with other code:

orip
  • 66,082
  • 20
  • 111
  • 144
4

For reference, node.js

Bnicholas
  • 12,007
  • 6
  • 21
  • 26
2

I've heard good things about Jaxer.

By the far the most popular/attractive part of these server-side javascript solutions is in regards to data validation. You can use the same code that you use to validate forms client-side again on the server to ensure their integrity. This comes really useful in simply being DRY and not getting rules out of sync when something changes.

Paolo Bergantino
  • 449,396
  • 76
  • 509
  • 431
2

As well as VBScript, classic ASP pages can use JScript as the underlying script language. You can run JScript programs on the Windows command line by using CSCRIPT.EXE. In fact, it's the same scripting engine, and it's extensible to support any number of languages.

Roger Lipscombe
  • 81,986
  • 49
  • 214
  • 348
1

SpiderMonkey and Rhino seem to be pretty much related. SpiderMonkey seems to be a C-library for executing JS, while Rhino is a Java-ditto.

Henrik Paul
  • 63,711
  • 30
  • 82
  • 93
1

Yes, JavaScript Virtual Machines exists outside of the browser. Here's a list of specially tailored server side adaptations.

I personally use it Spidermonkey on the command line to try code out. Rhino is the same adaptation of ECMAScript as Spidermonkey (same language implementation) but Rhino runs on the Java VM, and Spidermonkey was written in C.

Zach
  • 23,076
  • 9
  • 40
  • 50
0

As have been noted, Rhino is a JavaScript engine written in Java.

Java 6 has a new scripting engine facility, appropriatedly called the Java Scripting API in the javax.script package, for which a Rhino-based engine is available for use out of the box. I'm not sure if this is available on Java EE, but it is a standard feature of Java SE.

Using the Scripting API is quite simple and allows for Java applications to run scripts written in Javascript quite easily. If you require scripting in your application, there's no need to write your own interpreter -- just use the Scripting API. Also, the script engine has access to the Java class libraries, so it integrates nicely to perform tasks within your application as well.

For example, calculating the square root of 4, using the Scripting API is as simple as:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval("var x = java.lang.Math.sqrt(4);");
engine.eval("println(x);");

In the context of using this on the server side, I can envision an external Jaavscript being executed by Rhino and the results are then embedded onto the dynamically-generated HTML page that is being served. This way, features could be added or modified without making changes to the web application itself. The Javascipt scripts themselves can act more or less like plugins for the main application.

The Java Scripting Programmer's Guide has several examples which are built up step-by-step to introduce the features of the Java Scripting API.

Javascript doesn't necessarily have to be confined to a browser. As with any scripting language, it really depends on where the scripting engine is being hosted.

coobird
  • 151,986
  • 34
  • 204
  • 225
0

jslibs is a good browserless JavaScript runtime (based on Firefox's JS engine)

Franck Freiburger
  • 21,662
  • 20
  • 60
  • 90
0

There is also HTTPUnit which is more of a browser simulator, so, it allows you to interact with the DOM.

http://httpunit.sourceforge.net/

Brian Whitlock
  • 151
  • 2
  • 4
0

Have you checked out John Resig's post about creating a serverside Javascript engine with Rhino?

(http://ejohn.org/projects/bringing-the-browser-to-the-server/)

leeand00
  • 23,306
  • 34
  • 125
  • 265
0

choosing an engine

the other answers already mentioned several JS engines. one very important factor in deciding which one to use should be the language in which it is implemented (C, C++ or Java are the choices), since this "host language" will be the one you have access to very easily.

for example if you use rhino, you can easily access the whole java standardlib from javascript code (typically you will write wrappers in JS so you don't have all those java-lib calls sprinkled in your JS code).

choosing a framework

there were several mentioned alread. I like helma most. old and tried. lots of big sites run on it. there is also a sexy but very alpha version of helma over at github: http://github.com/hns/helma-ng/

serverside JS

you might also be interested in this group, they try to unify how JS works serverside (provide a standardlib, etc). the just started this group, but have a lot of smart people working on serversideJS for years:

https://wiki.mozilla.org/ServerJS

oberhamsi
  • 1,253
  • 7
  • 18