Skip to content

Tutorial: "Hello World"

colingourlay edited this page Sep 16, 2010 · 14 revisions

This example will help you write your first jimi app1.

In your project directory create 3 files: app.js, urls.js & handlers.js, and add the following code to them.

app.js

require('jimi').run({
    url_conf: require('./urls')
});

urls.js

var handlers = require('./handlers');

exports.mappings = [
    ['^$', handlers.hello_world]
];

handlers.js

var jimi = require('jimi');

exports.hello_world = function (req, res) {
    jimi.respond(res, 'Hello World');
}

Now, in your terminal, navigate to your project directory and run the following command:

node app.js

Now point your browser at http://127.0.0.1:8009/ and you should see “Hello World” in the window.

So let’s see what the code actually does…

app.js

require('jimi').run({
    url_conf: require('./urls')
});

In app.js We create and execute a jimi app.
  1. require('jimi') imports the jimi library
  2. run() builds the app and starts the server.

run()‘s only parameter is a object containing options that we want to set for our app. Many of these have defaults, so in this instance we’re just passing in the relative path to our urls.js file (and declaring that it contains our base URL configration). Have a look at the other examples in the apps directory to see what other options can be set.

Now let’s look at urls.js, our app’s base URL configuration file:

urls.js

var handlers = require('./handlers');

exports.mappings = [
    ['^$', handlers.hello_world]
];

jimi requires that all URL configuration files export an array of URL mappings Each mapping contains the following:

  1. A regular expression that defines a URL
  2. A function that processes a request to that URL and returns a response, which we call an handler2

Because we are storing these handlers in a separate handlers.js file, we need to include that file before we can use its handlers – this can be see in the first line of urls.js. In this case We are mapping the root URL onto the function handlers.hello_world. Let’s have a look at that function in handlers.js now:

handlers.js

var jimi = require('jimi');

exports.hello_world = function (req, res) {
    jimi.respond(res, 'Hello World');
}

We’re importing jimi here again as we’re going to call upon its respond function to complete the request. Every handler should be exported so that it can be seen by external packages (such as urls.js). Every handler also needs to accept req and res parameters so that it can handle the HTTP request (although it can take further parameters parsed by the URL regex3).

So there you have it. Your first app. Have a look at the other examples if you feel like digging deeper.

Notes

1 We’re assuming you’ve installed jimi and connect using npm and are using at least node v0.1.100

2 We are not restricted to mapping URLs to handlers. We can also map them to another URL mappings array in another file, which jimi will compile into more complex URLs. The basic and templating example apps highlight this functionality.

3 To see this in action……….you guessed it……….look at the example apps!

Clone this wiki locally