How can I use Director as a router in expressjs

I want to use express.js with the director of Flatiron (router) and resource-intensive (ODM), because I need the advantages of routing tables and cleaning multi-disk schemes with validation. The reason I have now completely switched to Flatiron is because I think she needs a little more time, and there is not much material.

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

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

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

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

Unfortunately, this does not work and gives me just "Can not GET /"

So what to do?

+5
source share
2 answers
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 .

, IR#nodejitsu freenode.

+5

-, , , , :

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();
  });
};

: , , express ( / ).

+3

All Articles