JSSP – JavaScript Server Page engine on top of NodeJS (Alligator)


I would like to introduce Alligator, a JSP/PHP/ASP-style template engine for JavaScript on top of NodeJS.

Most web-apps have been written in Java, PHP, ASP.net, Ruby or Python, on the server side, and JavaScript on the client side.

This approach of using different technologies and different programming languages for the different “sides” has few drawbacks:

1) Serialization of objects from one language to another has to be taken care of , e.g. take a look at GSON, a library that translates Java-to-JSON and vice versa.

2) Client-side code can’t be reused on the server-side and vice versa.

3) Web-apps “factories” has to hire different people with different expertises.

4) I bet you can think of at least 2 more drawbacks

Other than that, the multi-threaded approach that most of the current web-server/application-server utilize is problematic for load since it is limited by the maximum number of OS thread per machine.

NodeJS solved the later issue, by taking advantage of asynchronous I/O and the event-loop paradigm ( I will blog about it later on.. )

Moreover, NodeJS, let you write JavaScript code that runs outside a browser (utilizing the V8 runtime) + it includes many useful built-in Async I/O libraries as HttpServer and File System I/O management.

In the last few weeks, I have developed Alligator on top of this top-notch technology.

Alligator enable you to write PHP-style code but in the coolest and most popular language, JavaScript!!! 🙂

Let’s “Hello Word”-ing using Alligator:

<HTML>
   <BODY>
   <?="Hello World"?>
   </BODY>
</HTML>

In this example the <?= code?> just print whatever code.toString() evaluates to, yet it does that on the Server-Side..

Lets take a look at something a bit more “complex”:

<HTML>
   <BODY>
      <?  var test = 1;
          if (1 == test){?>
          YOOOO!!!!
          <?}?>
   </BODY>
</HTML>

Well, it’s pretty straight forward, the <? code ?> get rendered on the server-side, which mean that if test equals 1
then the String “YOOOO!!!!” will be written to the HTTP response.

Alligatror supports 3 type of object scopes, request, session and application, both session and application support set() and get() methods that receive key, value (if needed) and a callback function.  **One must pay attention to the fact that a callback function might get invoked later on in time regardless it’s chronological line in the code block.

The request parameters can be accessed easily using request.parameters.myKey..

Lets see the counter example which count how many people have been visit this page since the application is alive.

In this example, we have separated the logic and view and used the “commands.forward() built-in command” in order to pass the logic outcome to the view..

logic.jssp:

<?
	var counter = 1;
	application.get("counter",function(value){
		log.debug("ApplicationLOGIC.JSSP, value - " +value);
		if(value == undefined){
			application.set("counter",1);
		}else{
			counter = value+1;
			application.set("counter",counter);
		}
		request.parameters.counter = counter;
		commands.forward("counter/view.jssp");
	});
?>

view.jssp:

<HTML>
	<HEAD><TITLE>Application Scope Counter Tester</TITLE></HEAD>
	<BODY>
	<?
		var counter = request.parameters.counter;
		if(counter==1)
			commands.write("First Time");
		else
			commands.write("Number of users:" + counter);

	?>
	</BODY>
</HTML>
 

The main drawback of NodeJS is that it utlize only O(1) threads per instance, which mean that only one core can be used in a multi-core CPU system, in order to solver it, Alligator is using multi-node and memcached.

Multi-node duplicates the Node process as many time as you want, the smart thing to do is to set it to the number of available cores.

Memcached served as a shared memory between those processes.

One can easily set up Alligator to “work” on a distributed network, using a load balancer.

This is pretty existing, isn’t it? How can you start using it?
1 ) Install NodeJS – http://nodejs.org/#download

2 ) Extract Alligator somewhere – http://github.com/mrohad/Alligator/downloads

3 ) Setup alligator, by modifying settings.json -> read more here – http://github.com/mrohad/Alligator
the main thing you need to set is the root folder and the port.
I must mention that Alligator is built on top of anti-node and serves static files as well!

One can setup the server-side tags notations, for instance if you are a Java-person you can change the <?= code?> notation to <%=code%> in a super easy fashion.

Also, Alligator enables you to change the extension of the server-side script files, the default is JSSP.

If you would like to utilize a multi-core architecture, you should download memcached and set up the memcached values ( server, port ..)

Example for a setting file:

{
	"web_app_name" : "Alligator Test App",
	"port"         : 80,
	"path"         : {
				"root":"/home/vadmin/ws/Alligator/WWW/",
				"lib":"/home/vadmin/ws/Alligator/WWWlib/"
			},
	"server_script": {
				"ext":"jssp",
				"begin":"<?",
				"begin_additional_write":"=",
				"end":"?>",
				"session_minutes":30,
				"memcached":{
						"enable":1,
						"server":"localhost",
						"port":11211
					}
			 },
	"debug_mode"   : 1,
	"nodes"	      : 2
}

5 ) Run memcached ( if needed)

6 ) Run Alligator – $ sudo node alligator.js

7 ) Add your first .jssp file to the root folder

8 ) Test it , go to http://yourServer:port/yourfile.jssp

We are looking for people who would like to contribute code and suggest featuressssssss 🙂

Enjoy!!

3 thoughts on “JSSP – JavaScript Server Page engine on top of NodeJS (Alligator)

  1. Pingback: Asynchronous Semaphore « Simple Notions

  2. Pingback: Oscar's History

Leave a comment