Hello World with Narwhal and Jack

18 Nov 2009 | By Alex Young | Tags server tutorials

Narwhal allows you to create local or server-side JavaScript applications. It’s made by 280north who also developed Atlas. Narwhal is a beast:

  • Narwhal itself is a platform that’s designed to be multi-interpreter and cross-platform
  • Rhino has the most complete interpreter support
  • Narwhal has a standard library that conforms to the CommonJS standard
  • A package manager is provided, called Tusk

This tutorial is based on the hello-web example from the Narwhal documentation. You’ll build a tiny web app that can return text, all from JavaScript.

Installation

Downloaded it from the narwhal repository. There’s a zip file, or just use git:

git clone git://github.com/tlrobinson/narwhal.git

Environments

Narwhal makes a JavaScript interpreter and several scripts (including tusk) available. To get your shell into a suitable state, run bin/sea from the narwhal directory. Confirm everything works by typing narwhal.

Tusk and Jack

Tusk can install remote packages. Try typing tusk search json to view some JSON-related packages. Typing tusk install package-name will install a package.

Let’s use tusk to create our own app:

tusk init hello-web
cd hello-web

Then install jack:

tusk install jack

This command added jack to your hello-web project. Jack makes communicating with web servers easier — it’s based on highly successful projects from the Ruby and Python communities.

Hello, world

Jack expects a file called jackconfig.js. Create one that looks like this:

exports.app = function(env) {
  var text = "Hello, world!";
  return {
    status : 200,
    headers : { "Content-Type" : "text/plain", "Content-Length" : String(text.length) },
    body : [text]
  };
};

From the same directory run jackup and visit http://127.0.0.1:8080/.

Documentation

Narwhal comes with documentation in the docs/ folder. There’s enough in there about packaging, the standard library and narwhal’s environment to keep you busy for a while.


blog comments powered by Disqus