"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,
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.
Thanks Peter. That was indeed my problem.
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
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
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.
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.
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
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.
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.
I updated and it worked. Thanks Peter.
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.
feed
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)