Mark Gandolfo

Here are some thoughts of mine.

How to read a file using clojure

Today I needed to read a small file into a vector so I can iterate over it line by line. Here is the small function to do this.

1
2
3
4
(defn read-file [f]
  "Read in a file and return the result"
  (-> (slurp f)
    (clojure.string/split #"\n")))

And then to use

1
(println (read-file "resources/input.txt"))

Clojure Blog Tutorial

Lets create a simple blog in clojure. I think it should

  • Adhere to an MVC standard
  • Have an Admin interface with basic authentication
  • be backed by MySQL
  • have Views/Templates (we’ll use mustache)

The source code can be found on Github

First install Leiningen so we can start to build a clojure app!

Create the project

$ lein new compojure blog

Compojure is a nifty clojure library that provides some routing. It’ll parse the path in the URL and call a function. The above command will give us a basic scaffold which we can begin to build our app around. The most important directory is the /src directory, this is where we’ll put all of our code.

First lets start the server

$ lein ring server

Go to http://localhost:3000 in your browser and make sure it works. You should see a Hello World message!

Lets get started!

First lets run through the target state anatomy of the app

There will be a couple of files and directories we will work with to adhere to MVC, most aren’t there yet, but we’ll create them as we move forward

/src/blog/handler.clj

Our routes will go in handler.clj, this is also the main file that gets called when we run our server. It’ll look like this after the generation

/src/blog/handler.clj
1
2
3
4
5
6
7
8
9
10
11
12
(ns blog.handler
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]))

(defroutes app-routes
  (GET "/" [] "Hello World")
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (handler/site app-routes))

/src/blog/controllers/

This directory will hold all of all controllers and actions which will be called from the handler.clj routes file

/src/blog/models/

This directory will hold the models which will store business logic and more importantly a gateway to external data, be it from a database, document store or a web service.

/resources/views/

This directory will hold our moustache views.

Routes

In the /src/blog directory you should see a handler.clj file, this is where we’re going to store our routes, and we’ll create a few directories where we store controllers, models and views. We’ll do this a little in this tutorial.

If you open up handler.clj you’ll find a route

1
(GET "/" [] "Hello World")

Breaking this down, it’s listening to a GET request, at path “/” in the URL, it’s not expecting any params [] to be past through and it’ll return the response “Hello World”. You could change “Hello World” to be another string, reload your browser to see the change.

Create a posts controller & index action

Each controller will respond to several RESTful actions, “index, show, new, create, edit, update, destroy”. If coming from an MVC framework these will be very familiar, if not hang in there, it’ll make sense soon.

Lets start by create the directory /src/blog/controllers/ and a new file in that directory called `posts.clj’.

For now, lets start by creating a function that we’ll use to respond to the root path “/”.

/src/blog/controllers/posts.clj
1
2
3
(ns blog.controllers.posts)

(defn index [] "Hello, from index")

Index action to start with

Now lets tell the route “/” to use the newly created index action in the posts controller. To do this we’ll do two things in the handler.clj

  1. Import the controller so all actions/functions are available in our handler.clj file
  2. In place of the string “Hello World” have the “/” route use our new index action

Below are the two changes:

/src/blog/handler.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
(ns blog.handler
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]
            [blog.controllers.posts :as posts-controller])) ;<-- add this line

(defroutes app-routes
  (GET "/" [] (posts-controller/index)) ;<-- update this line
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (handler/site app-routes))

Start the server again and load the app, you should now see “Hello, from index”

$ lein ring server

Create the template for the posts, index controller

So that was easy, with a few lines of code we’ve managed to render a controller with a string, although easy, it wasn’t very impressive.. It would be much more impressive if we could render a template file full of html in place of our “Hello, from index” String.

To do this there will be four things we need to do:

  1. Add the clostache to the project dependencies
  2. Create the mustache template
  3. Add some render helper methods
  4. Modify the posts controller index function to render the template

Lets do it.

In the project.clj file at the root of your project you need to add the dependency for clostache. This will let lein know what dependencies to download to run the application

/project.clj
1
2
3
4
5
6
7
8
9
10
11
(defproject blog "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [compojure "1.1.6"]
                 [de.ubercode.clostache/clostache "1.3.1"]]  ;<-- add this line
  :plugins [[lein-ring "0.8.10"]]
  :ring {:handler blog.handler/app}
  :profiles
  {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
                        [ring-mock "0.1.5"]]}})

Next lets create the directory /resources/views/posts/ where we’ll store the templates and create the template index.mustache

/resources/views/posts/index.mustache
1
2
3
4
5
6
7
8
9
10
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <div class="container">
            <h1>Hello, {{ name }}</h1>
        </div>
    </body>
</html>

Add the render methods:

In order to keep the codebase DRY we should be adding these to a controller-helper file, but for the purpose of this tutorial we’ll add them to the /src/blog/controllers/posts.clj file

The below functions will read a template and render it, beware that if the file doesn’t exist it’ll render a

/src/blog/controllers/posts.clj
1
2
3
4
5
6
7
8
9
10
11
12
(ns blog.controllers.posts
  (:require
    [clostache.parser :as clostache]))

(defn read-template [template-name]
  (slurp (clojure.java.io/resource
    (str "views/posts/" template-name ".mustache"))))

(defn render-template [template-file params]
  (clostache/render (read-template template-file) params))

...

Now simply we’ll update the index action in our posts controller, and swap out the "Hello, from index" to the below

/src/blog/controllers/posts.clj
1
2
3
4
...

(defn index []
  (render-template "index" {:name "clojure developer"}))

Reloading the webpage you should see that our HTML has rendered with our template replacement variable “name”

But what about the data? Lets add a model

Here we’ll need to do a few things:

  1. Add the mysql dependencies in project.clj
  2. Create a db, and a posts table
  3. Create a posts model
  4. Fill it with some methods to help pull content out of a db
  5. update the index.mustache file
  6. update the posts.clj to pass to use the model

Migrations are out of scope of this tutorial, so create a blog database with a posts table, the table should have the following fields:

1
2
3
4
5
id:integer
title:varchar(254)
body:text
created_at:datetime
updated_at:datetime

Now we have a database, add the following jdbc & mysql connector dependencies to our project.clj file

/src/project.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
(defproject blog "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [compojure "1.1.6"]
                 [org.clojure/java.jdbc "0.3.0-alpha5"]       ;<-- add this line
                 [mysql/mysql-connector-java "5.1.25"]        ;<-- add this line
                 [de.ubercode.clostache/clostache "1.3.1"]]
  :plugins [[lein-ring "0.8.10"]]
  :ring {:handler blog.handler/app}
  :profiles
  {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
                        [ring-mock "0.1.5"]]}})

Next lets add the directory /src/blog/models/ with the file posts.clj

In the posts.clj file we need to add some requires statements, the mysql-db connector and the all function.

/src/blog/models/posts.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(ns blog.models.posts
  (:refer-clojure :exclude [get])
  (:require [clojure.java.jdbc :as j]
            [clojure.java.jdbc.sql :as s]))

(def mysql-db {:subprotocol "mysql"
               :subname "//localhost:3306/blog"
               :user "root"
               :pass " "
               :zeroDateTimeBehaviour "convertToNull"})

(def now
  (str (java.sql.Timestamp. (System/currentTimeMillis))))

(defn all []
  (j/query mysql-db
    (s/select * :posts)))

Update the index.mustache file to show the blog posts

/resources/views/posts/index.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
  <head>
    <title>Clojure Blog</title>
  </head>
  <body>
    <div class="container">
      <h1>Listing Posts</h1>
      <sections>
      {{#posts}}
        <h2>{{title}}</h2>
        <datetime>{{created_at}}</datetime>
        <p>{{body}}<p>
      {{/posts}}
      </sections>
  </div>
  </body>
</html>

Update the posts.clj controller to require the posts-model and change the index action

/src/blog/controllers/posts.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(ns blog.controllers.posts
  (:require
    [clostache.parser :as clostache]
    [blog.models.posts :as posts-model]))                       ;<-- update this list

(defn read-template [template-name]
  (slurp (clojure.java.io/resource
    (str "views/posts/" template-name ".mustache"))))

(defn render-template [template-file params]
  (clostache/render (read-template template-file) params))

(defn index []
  (render-template "index" {:posts (posts-model/all)}))         ;<-- update this list

In your favourite sql editor, add a few test posts. Reload the browser and you should see the posts!

Lets set up an admin controller

What’s the point of having a blog without an admin? Hang on now, we’ll be going a little bit faster with less explanation. But if you’ve got this far I think you’ll get it!

What we’ll be doing here is:

  1. Add an admin controller with CRUD actions
  2. Create the admin routes in a protected-routes function
  3. Add basic auth to the protected-routes
  4. Add the Admin views

Lets add the /src/blog/controllers/admin/posts.clj controller with the actions required to CRUD.

/src/blog/controllers/admin/posts.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(ns blog.controllers.admin.posts
  (:require
    [clostache.parser :as clostache]
    [blog.models.posts :as posts-model]))

(defn read-template [template-name]
  (slurp (clojure.java.io/resource
    (str "views/admin/posts/" template-name ".mustache"))))

(defn render-template [template-file params]
  (clostache/render (read-template template-file) params))

(defn index []
  (render-template "index" {:posts (posts-model/all)}))

(defn show [id]
  (render-template "show" {:post (posts-model/get id)}))

(defn edit [id]
  (render-template "edit" {:post (posts-model/get id)}))

(defn new []
  (render-template "new" {}))

Updating the handler.clj file to include basic auth, protected and public routes as well as a group of the admin/posts routes, you’ll notice we ommited the create/update actions, and instead handle it in the router with the do function. This is personal choice and won’t always be practical. i.e. if you need to add validation, etc.

/src/blog/handler.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(ns blog.handler
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]
            [ring.middleware.basic-authentication :refer :all]
            [ring.util.response :as resp]
            [blog.models.posts :as posts-model]
            [blog.controllers.posts :as posts-controller]
            [blog.controllers.admin.posts :as admin-posts-controller]))

(defn authenticated? [name pass]
  (and (= name "user")
       (= pass "pass")))

(defroutes public-routes
  (GET "/" [] (posts-controller/index))
  (route/resources "/" ))

(defroutes protected-routes
  (GET "/admin" [] (admin-posts-controller/index))
  (GET "/admin" [id] (admin-posts-controller/show id))
  (GET "/admin/posts/new" [] (admin-posts-controller/new))
  (POST "/admin/posts/create" [& params]
    (do (posts-model/create params)
        (resp/redirect "/admin")))
  (GET "/admin/posts/:id/edit" [id] (admin-posts-controller/edit id))
  (POST "/admin/posts/:id/save" [& params]
    (do (posts-model/save (:id params) params)
        (resp/redirect "/admin")))
  (GET "/admin/posts/:id/delete" [id]
    (do (posts-model/delete id)
      (resp/redirect "/admin"))))

(defroutes app-routes
  public-routes
  (wrap-basic-authentication protected-routes authenticated?)
  (route/not-found "404 Not Found"))

(def app
  (handler/site app-routes))

Now lets add basic auth dependency in our project.clj file

/blog/project.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(defproject blog "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [compojure "1.1.6"]
                 [org.clojure/java.jdbc "0.3.0-alpha5"]
                 [mysql/mysql-connector-java "5.1.25"]
                 [de.ubercode.clostache/clostache "1.3.1"]
                 [ring-basic-authentication "1.0.2"]]         ;<-- add this line
  :plugins [[lein-ring "0.8.10"]]
  :ring {:handler blog.handler/app}
  :profiles
  {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
                        [ring-mock "0.1.5"]]}})

Next we’ll add the admin views.

/resources/views/admin/posts/index.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<html>
  <head>
    <title>Clojure Blog</title>
  </head>
  <body>
    <div class="container">
      <h1>Listing Posts</h1>
      <a href="/admin/posts/new">Add</a>
      <sections>
      {{#posts}}
        <h2><a href="/admin/posts/{{id}}">{{title}}</a></h2>
        <datetime>{{created_at}}</datetime>
        <p>{{body}}<p>
        <p>
          <a href="/admin/posts/{{id}}/edit">Edit</a>
          <a href="/admin/posts/{{id}}/delete">Delete</a>
        </p>
      {{/posts}}
      </sections>
    </div>
  </body>
</html>
/resources/views/admin/posts/show.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html>
  <head>
    <title>Clojure Blog</title>
  </head>
  <body>
    <div class="container">
      <h1>Listing Post</h1>
      <sections>
        <h2>{{title}}</h2>
        <datetime>{{created_at}}</datetime>
        <p>{{body}}<p>
        <p>
          <a href="/admin/posts/{{id}}/edit">Edit</a>
          <a href="/admin/posts/{{id}}/delete">Delete</a>
        </p>
      </sections>
    </div>
  </body>
</html>
/resources/views/admin/posts/new.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
  <head>
    <title>Clojure Blog</title>
  </head>
  <body>
    <div class="container">
      <h1>Editing Post</h1>
      <form action="/admin/posts/create" method="post">
        <label for="title">Title</label>
        <input name="title"/><br />
        <label for="body">Body</label>
        <textarea name="body"></textarea> <br/>
        <input type="submit"/>
      </form>
    </div>
  </body>
</html>
/resources/views/admin/posts/edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
  <head>
    <title>Clojure Blog</title>
  </head>
  <body>
    <div class="container">
      <h1>Editing Post</h1>
      <form action="/admin/posts/{{post.id}}/save" method="post">
        <label for="title">Title</label>
        <input name="title" value="{{post.title}}"/><br />
        <label for="body">Body</label>
        <textarea name="body">{{post.body}}</textarea> <br/>
        <input type="submit"/>
      </form>
    </div>
  </body>
</html>

Finishing up!

There’s still a lot of work to do, such as styling, rendering of partials, DB based user accounts, and importantly DRYing up the code.

The source code can be found on Github

Thanks for reading.


Switching between activities in an android app

To move between activities in Android development you can simply use intents.

1
2
3
// new Intent(CurrentActivity instance, targetActivity class)
Intent i = new Intent(MainActivity.this, ResultActivity.class);
startActivity(i);

Before starting the activity we can even send some information around

1
2
3
Intent i = new Intent(MainActivity.this, ResultActivity.class);
i.putExtra("label", "A value which will be passed");
startActivity(i);

And then in your ResultActivity you can fetch the information

1
myLabel = getIntent().getStringExtra("label");

But what happens if we want to do something tricky? Perhaps from the MainActivity load the ResultActivity but automatically take the user back to the MainActivity.

It’s easy, you simply create a new runnable with a timeout as the last argument. In the below case, it’s 2000ms (2 seconds).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ResultActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // do your usual on create stuff!
    new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
        final Intent mainIntent = new Intent(ResultActivity.this, MainActivity.class);
        ResultActivity.this.startActivity(mainIntent);
        ResultActivity.this.finish();
      }
    }, 2000);

  }
}

Maintain state of an android application

I’ve recently been getting involved in a little bit more android application development, and I was curious on the different ways state can be maintained in an android application on screen rotation, or between activities. Here is the design pattern I’ve found most effective.

Each android application extends from the Application, it shares various characteristics of a Singleton, but fundamentally isn’t a Singleton in the traditional sense. There are various discussions on stackoverflow discussing the finer semantics of this, but we’ll just accept that this works, and begin.

By extending the Application class we can store application specific information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Game extends Application {
  private int score = 0;
  private boolean gameLoaded = false;

  public Game() {
    if(!gameLoaded) {
      // Init your game, build isometric tile maps, or start to define your level
      gameLoaded = true;
    }
  }

  public void incrementScore() {
    score++;
  }

  public int getScore() {
    return score;
  }
}

Then in your Activity you can simple call getApplicationContext() which returns a game object to store state, and interact.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MainActivity extends Activity {
  View activityMain;
  TextView message;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Game state = ((Game) getApplicationContext());
  }

  button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
      Game state = ((Game) getApplicationContext());
      state.incrementScore();
      message = (TextView) findViewById(R.id.message);
      message.setText(state.getScore());
    }
  });
}

This is by far the easiest way to store state of your application which isn’t re-run everytime during screen rotation (orientation changes) or loading different views.


Date & Time Helper with rspec and capybara

A set of simple helpers for you to place into ./spec/helpers/helpers.rb, I searched for a while and couldn’t find an elegent solution, so I quickly whiped this up and thought I’d share incase anyone else needed a hand.

The first helps select a date from a date and time rails widget

1
2
3
4
5
6
7
8
def select_date_and_time(date, options = {})
  field = options[:from]
  select date.strftime('%Y'), :from => "#{field}_1i" #year
  select date.strftime('%B'), :from => "#{field}_2i" #month
  select date.strftime('%d'), :from => "#{field}_3i" #day 
  select date.strftime('%H'), :from => "#{field}_4i" #hour
  select date.strftime('%M'), :from => "#{field}_5i" #minute
end

To use it, in your capybara rpsec tests, you can find use it by

1
select_date_and_time(DateTime.now, from:published_date)

Similarly the following helper selects just a date

1
2
3
4
5
6
def select_date(date, options = {})
  field = options[:from]
  select date.strftime('%Y'), :from => "#{field}_1i" #year
  select date.strftime('%B'), :from => "#{field}_2i" #month
  select date.strftime('%d'), :from => "#{field}_3i" #day 
end

And use it as follows in your tests

1
select_date(Date.today, from:published_date)

Cups & HP-Lib on raspberry Pi to create a new paperless life

I tend to hoard paper. It’s not something I like doing, but I have a hard time differentiating the important bills from the non important, so I tend to keep everything which usually piles up until it falls over and I spend a day going through it, throwing out most things. There must be a solution to push all of these documents in a digital format to one of the many services online (read dropbox/google drive). Although I’m aware doxie exists, about this time I bought a raspberry Pi, and have had a ball making LED’s flash, but I couldn’t help thinking that there must be a better use for it. So, I think you all know where I’m going with this. My plan: Raspberry Pi to be connected to my HP coloursmart c7280 all-in-one device (of which the printer no longer works, so it’s only use is now a scanner). I’ll wire up (via breadboard) a number of switches & buttons. 6 switch, dip Switch will determine document type (it’ll light a specific LED – I’ll have printed labels next to the LED’s) The Button will initiate the process which will:

  • Start the scan
  • Rename the document (This part is still needs to be solved)
  • Upload the document
  • Clean up

Capybara, RSpec, Swinger and a dash of awesome

When trying to get Capybara-rspec working I went googling first, to see how others have done it… People suck and generally over complicate the simplest tasks… Heres how I got it all working!

Gemfile
1
2
3
gem 'rspec-rails'
gem 'capybara'
gem 'swinger'

Add rspec rails, capybara and swinger, then install your gems and generate rspec skeleton files

1
2
bundle install
rails g rspec:install

In your spec/spec_helper.rb file require your gems.. a quick note, I force spec to use “test” env, why wouldn’t it do this by default? I’m sure theres an answer, but it doesn’t feel write, and everything works just fine like this!

spec/spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
ENV["RAILS_ENV"] = 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'swinger'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
end

I really like the idea of my integration tests living in a directory called “spec/integration”, so I create it, and put some tests in there.

/spec/integration/user_integration.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
require "spec_helper"

describe 'A user session' do

  it 'should allow a user to login', :driver => :selenium do
    visit '/'
    click_link 'Login'
    fill_in 'Email', :with => "user@markyg.com"
    fill_in 'Password', :with => "password"
    click_button 'Sign in'
    find('Signed in successfully')
  end
end

A quick note, swinger allows us to add :driver => :selenium, or :driver => :culerity to our specs, this is how we can easily switch drivers.. if you leave this out, it’ll use rack.


Raw Output Of Html In Rails3

As of rails3 the <%=h tag is now applied by default to all of your <%= @content So for instance in a message:

1
2
3
4
5
# Controller
@message = "Unfortunately an error occurred: Sorry, this product is no longer available, if you think you received this in error, please [contact us](/contact/us)"

# View
= @message

would result in Unfortunately an error occurred: Sorry, this product is no longer available, if you think you received this in error, please <a href='/contact/us'>contact us</a> To remove htmlentities being sanitized you can use the raw() method helper

1
2
3
4
5
# Controller
@message = "Unfortunately an error occurred: Sorry, this product is no longer available, if you think you received this in error, please [contact us](/contact/us)"

# View
=raw @message

would result in Unfortunately an error occurred: Sorry, this product is no longer available, if you think you received this in error, please [contact us](/contact/us) Be careful how you use this, if @message was set from user input, than they could inject some nasty javascript or flash in your page.


Rails3: Undefined Method Includes Values

I can across an error in Rails3 today when I was trying to implement a scope.

1
scope :latest, order("created_at asc").first

When trying to run that Scope I’d get undefined method 'includes_values' for <#Product:rarrarrar> It had me scratching my head since the trace showed me nothing. So I went through the activerecord codebase, and sure enough it was something stupid that I did. The scope should return an ActiveRecord object, and not a result set. So simply removing the .first off my scope resulting in a working scope

1
scope :latest, order("created_at asc")

Its a simple fix, but googling came up with nothing. So I hope this helps someone else who comes across this error.


How To: Install Node Js And Roll Out Your First Project

So after NodeKnockout I have to say I learnt a lot about the simplicity that comes with developing a nodeJs app! A few people have asked me about how easy it is to roll out their own app, and I’ve walked a few people though it, and finally decided it warranted a blog post! You’ll be walked through how to set up:

Quick Note

I’m doing this on a mac, but it should be the same on any posix system (linux, bsd, etc) (sorry windows, but if you want to be a serious developer, and .Net isn’t doing it for you, then change your OS) So lets get this started!

Install NodeJS

Visit the NodeJS website and download the latest node package. It’ll come in tar.gz format. At the time of writing this, its node-v0.2.0.tar.gz. So I’ll pull it down, and install it! On the console do:

1
2
3
4
5
6
wget http://nodejs.org/dist/node-v0.2.0.tar.gz
tar xvf node-v0.2.0.tar.gz
cd node-v0.2.0
./configure
make
sudo make install

The sudo is required as node installs /usr/local/lib/ Once thats done, check to make sure you have it installed

1
node --version => 0.2.0

Install NPM

Visit the NPM site. There is a link to the github repo that has some extra info not on the site. I recommend visiting it and reading the readme.. After you’ve done that, to install:

1
curl http://npmjs.org/install.sh | sudo sh

And then check if its installed

1
npm --version => 0.1.27-11

You might have a later version, again this was the latest at time of writing.

Express

Express is a sinatra like framework that wraps nodejs. It looks pretty sexy! It uses Jade by default, which is a HAML like templating language. Very sexy indeed.

1
2
sudo npm install express
sudo npm install jade

Rolling out a new nodejs app!

Express has a few generators, which makes life easier for all involved.

1
express my_new_project

Go through the project and have a look at the directories/files. The important one there is the app.js file. This file is used to run your new project! To run your node server run

1
node app.js

You can then point the browser at http://localhost:3000 and bingo!

Where to find packages?

There is a good list of hosted Addons & Modules. Or just a bit of googling will come up with a few extras. In further articles I’ll write a little bit more about how to modify the app, add extra actions, etc.