5

I want to use express.js with Flatiron's director (router) and Resourceful (ODM) because I need like the benefits of routing tables and clean multi-db schemas with validation. The reason why I now completly switch to Flatiron is, is because I think it needs some more time and there is not much doc material.

However, that is the current way I use director in express:

var express = require('express')
  , director = require('director');

function hello(){
    console.log('Success');
}

var router = new director.http.Router({
    '/': {
        get: hello
    }
});

Unfortunatly this doesn't work and gives me just a "Cannot GET /"

So what's to do?

dev.pus
  • 7,919
  • 13
  • 37
  • 51
  • Added another routing variant example to the repo to show how you can easily build on top of in your own app or an extension https://github.com/visionmedia/express/commit/39efa452fcb075a9a5870f1f71fe60a8742e687b – TJ Holowaychuk Jun 29 '12 at 16:02

2 Answers2

5
var express = require('express')
  , director = require('director')
  , http = require('http');

var app = express();

var hello = function () {
  this.res.send(200, 'Hello World!');
};

var router = new director.http.Router({
  '/': {
    get: hello
  }
});

var middleware = function (req, res, next) {
  router.dispatch(req, res, function (err) {
    if (err == undefined || err) next();
  });
};

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');

  app.use(express.favicon());
  app.use(express.bodyParser());

  app.use(middleware);

  app.use(express.static(__dirname + '/public'));
});

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

There is a sample app using express, resourceful and director here.

If you have more doubts, you can ask them in our IRC room #nodejitsu on freenode.

Pavan Kumar Sunkara
  • 3,025
  • 21
  • 30
3

First, in order to use director you need to wrap it up as a middleware and pass it to express, like so:

app.use(function (req, res, next) {
  router.dispatch(req, res, function (err) {
    if (err) {
      // handle errors however you like. This one is probably not important.
    }
    next();
  });
};

Aside from that: You don't need director to use resourceful, and express has its own router (so you may not even need/want director).

Josh Holbrook
  • 1,611
  • 13
  • 10
  • Hi josh, I have compared express and flatiron intensivly. The flatiron components have good thoughts (especially routing tables (director), one intuitiv schema api with validation and lots of possibilities to code the schema). These things are really heavy pros but on the other side there are things which break the intuition. For example the "complicated" way to use director (because of the wide support: server, client and cli, http) or the way to create a database connection. I hate to critisise this without having the know-how to contribute :( but maybe it helps the nodejitsus :) – dev.pus Jun 28 '12 at 18:23