"hello, world" Tutorial: Synergy Server-Side JavaScript

You are invited to try an early alpha JavaScript framework for writing web applications...

Install Packages on UNIX

You need some standard programs. On my Mac I have make v3.81, sudo v1.6.8p12, Perl 5.8.8.

Synergy is distributed as a collection of xpkg packages so first we must install the xpkg package management client.


curl -O http://xpkg.michaux.ca/releases/XPKG-0.01.tar.gz
tar xvzf XPKG-0.01.tar.gz
cd XPKG-0.01
perl Makefile.pl
make
sudo make install

Second, create a package set for the Synergy packages and install various Synergy packages.


xpkg create ourxjs \
            xpkg_source=http://xjs.michaux.ca/packages \
            xpkg_architecture=rhino \
            xpkg_architecture=all \
            libdir=/usr/local/lib/xjs \
            bindir=/usr/local/bin \
            libpath="." \
            libpath=/usr/local/lib/xjs
xpkg updatesources ourxjs
sudo xpkg install ourxjs SynergyGenerator

Check that the synergy command-line tool is now in your path.

$ which synergy
/usr/local/bin/synergy

If the above which command could not find the synergy program then add the following at the end of your ~/.bash_login file

export PATH="/usr/local/bin:$PATH"

and restart your terminal.

(Sorry Windows users. I don't know how Windows works. Perhaps Cygwin?)

Create the Synergy hello Web App Package

$ cd
$ synergy hello
$ cd hello
$ xws

Point your browser to http://localhost:3000/ to see a static welcome page.

You can leave the web server running until the end of the tutorial.

Add an action

Create a file ~/hello/lib/app/actions/greet.js with the following content.

pushAction(
  function(request) {
    return request.path == '/greet';
  },
  function(request) {
    return {
      body: 'hello, ' + (request.params.name || 'world')
    };
  }
);

Point your browser to http://localhost:3000/greet and http://localhost:3000/greet?name=you

Add a layout and view

Create a file ~/hello/lib/app/views/greet.ejs with the following content.

<p>hello, <%= c.name || 'world' %><p>

Create a file ~/hello/lib/app/views/layout.ejs with the following content.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>hello app</title>
</head>
<body>

  <%= c.content %>

</body>
</html>

Update ~/hello/lib/app/actions/greet.js to use the new layout and view.

pushAction(
  function(request) {
    return request.path == '/greet';
  },
  function(request) {
    return {
      body: layout({content:greet({name:request.params.name})})
    };
  }
);

Point your browser to http://localhost:3000/greet and http://localhost:3000/greet?name=you

ctrl-c to stop the web server.

Comments

Have something to write? Comment on this article.

Alex Robinson May 7, 2008

I can run the shell just fine, but after creating the hello project and

"Point your browser to http://localhost:3000/ to see a static welcome page."

I get:

ReferenceError: "main" is not defined. (/usr/local/lib/xjs/webServer/lib/handleRequest.js#206)

[solidgoldpig:~/Scripts/Javascript/Synergy] alexr% synergy hello
Shell class loaded
[solidgoldpig:~/Scripts/Javascript/Synergy] alexr% xws
environment = development
port = 3000
JettyEmbeddedApp class loaded.
2008-05-07 09:35:07.538::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
2008-05-07 09:35:07.541::INFO:  jetty-6.1.5
2008-05-07 09:35:08.670::INFO:  Started SocketConnector@0.0.0.0:3000
HarmonyRequestHandler class loaded
HarmonyRequestHandler instantiated
REQUEST /
Shell class loaded
looking for static file: /Users/alexr/Scripts/Javascript/Synergy/www/
looking for static file: /Users/alexr/Scripts/Javascript/Synergy/www/.html
looking for static file: /Users/alexr/Scripts/Javascript/Synergy/www//index.html
httpServletRequest.getRequestURL() = http://localhost:3000/
caught exception
Peter Michaux May 7, 2008

Alex,

Thanks for giving it a try. When you start the web server with xws you must be in the ~/hello directory. Just add a cd hello before xws and it should work.

Alex Robinson May 7, 2008

Thanks Peter. That was indeed my problem.

ryan May 9, 2008

No love on Ubuntu 7.10:

me@computer:~/temp$ synergy hello
Exception in thread "main" java.lang.NoClassDefFoundError: ca/michaux/xjs/Shell

The directory (hello/) was made, but:

me@computer:~/synergy/hello$ xws
environment = development
port = 3000
Exception in thread "main" java.lang.NoClassDefFoundError: ca/michaux/xjs/JettyEmbeddedApp
Peter Michaux May 9, 2008

Ryan,

It looks like you are not in the right directory when you start the web server with xws.

It looks like you did this

me@computer: /temp$ synergy hello
me@computer: /synergy/hello$ xws

but the second line looks like the wrong directory. It should be something more like this

me@computer: /temp$ synergy hello
me@computer: /temp/hello$ xws
Drew Franklin May 10, 2008

Hi Peter,

When I go to localhost:3000/greet or localhost:3000/greet?name=drew it immediately downloads the rendered ejs file. Is this an error on my end? I just checked out the most recent version of xjs.

Peter Michaux May 10, 2008

Drew,

I just did the tutorial with the most recent xjs modules. It seems to work for me. What do you mean by "it immediately downloads the rendered ejs file"? That kind of sounds like what is supposed to happen.

Drew Franklin May 10, 2008

Okay so it must be on my end. I copied the code right from the website but I will look deeper. By downloads it I mean immediately downloads the html file to my downloads folder and clears the url field like when you click a download link on source forge or something like that.

Thanks for your help

Peter Michaux May 10, 2008

Drew,

I see what you describe in Mac/Safari. It doesn't happen in Mac/Firefox or Mac/Opera. The server may be not adding a necessary header or adding an incorrect one. I'll look into it. Thanks.

Peter Michaux May 10, 2008

Drew,

It was a bad header. It should be working now if you update your svn working copy and reinstall (everything or just the webServer module). If you give it a try please let me know if it works or not.

Drew Franklin May 10, 2008

I updated and it worked. Thanks Peter.

pihentagy October 18, 2008

Hi!

Have your heard about jaxer? http://www.aptana.com/jaxer

How Synergy compares to it?

thanks

Peter Michaux October 18, 2008

pihentagy,

I have heard about Jaxer. Their 'script runat="server"' tags in the HTML pages seems in the realm of a PHP or JSP approach to page generation. Synergy is more along the lines of a MVC framework like Ruby on Rails. I believe Jaxer runs on the Mozilla Spidermonkey JavaScript engine while Synergy runs on the Mozilla Rhino JavaScript engine.

Have something to write? Comment on this article.