Introduction to Server-side JavaScript | WebReference

Introduction to Server-side JavaScript

By Rob Gravelle


[next]

Although rich web sites and applications have become the norm in recent years, they can still be difficult to develop, maintain, and expand. Many of the challenges stem from the schism between client and server components. Client-side components usually consist of HTML, CSS, JavaScript, Ajax, JS libraries, images, and whatever other files that are to be downloaded to the browser. On the server, you need a listener to process requests, fetch resources or information, and manipulate them so that they can be sent back to the client. This is usually accomplished using XML, JSON, or HTML-Formatted text, which is sent across the wire using Ajax. There are a number of competing technologies to choose from here. Depending on your traffic, hardware, O/S, bandwidth, IT expertise, and numerous other factors, there is a technology for every taste and occasion. Popular server-side languages at this time include PHP, Java, and .NET, to name only a few. There is presently a ServerJS movement whose goal is to eliminate the gap between client and server. Exponents of the group want to keep with HTML, JavaScript, and CSS, which are most familiar to the end-users. What makes server-side JavaScript possible is a web server than can process the code. One such server is called Jaxer. Developed by Aptana, Jaxer is an open source Ajax web server for building rich web pages and applications using a unified Ajax model that can be written entirely using JavaScript. Writing code for Jaxer is the focus of this article.

Some History on Server-side JavaScript

Server-side JavaScript (SSJS) refers to JavaScript that runs on server-side and is therefore not downloaded to the browser. This term is used to differentiate it from regular JavaScript, which is predominantly used on the client-side (also referred to as client-side JavaScript or CSJS for short). The first implementation of SSJS was Netscape's LiveWire, which was included in their Enterprise Server 2.0 back in 1996. Since then, a number of other companies have followed suit in offering an alternative to the usual server-side technologies. One of the biggest players in the field was Microsoft. They supported the use of JavaScript on the server within what is now knows as "classic" ASP. Along with the most common VBScript language, it also supported JavaScript and PerlScript. In reality, Microsoft utilized JScript, their own version of JavaScript. To use JScript/JavaScript, all you had to do was set the LANGUAGE attribute in the opening script tag:

Since the code runs on the server, what is sent to the client is the output of the script rather than the source code. Hence only the tags produced by the Response.Write() functions are found in the page source:

In addition to alleviating development complexity, server-side JavaScript offers a few other benefits that you may not have considered:

  • The same code can validate data on both the client (for immediate user feedback) and on the server (for security), so validations never get out of sync.
  • The same code can prepare both the HTML DOM server side and modify it client-side, when the user changes the data or it's refreshed from the server.
  • Using the same code on both the client and the server, developers have fewer technologies to learn and stay on top of, and fewer parts of the application or site to maintain.

The Aptana Jaxer Server

Jaxer's server-side engine is based on Mozilla Gecko, the same browser engine that's in the Firefox browser. The Mozilla engine allows web pages to be manipulated in the same way that client-side code can. Jaxer's server-side JavaScript APIs are even more powerful than client-side JavaScript in that they enable database access, file system access, network communications, user sessions, and other functions that are typically only found in web application languages. Jaxer even provides support to access Java objects via the DWR project.

Jaxer is not a stand-alone server, but rather, acts as a plug-in to another server such as Apache, Jetty or Tomcat so that it can handle the traffic load. Jaxer provides the server-side DOM and API processing for pages served by the web server before delivering the results to the browser.


[next]