Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flatiron.js routing and templating with union, director and plates?

Coming from express.js, I want to give flatiron a try for a small project. However, there are some small problems which keep me from actually getting somewhere.

var flatiron = require('flatiron')
,  session = require('connect').session
,  ecstatic = require('ecstatic')
,  path = require('path')
,  fs = require('fs')
,  plates = require('plates')
,  director = require('director')
,  winston = require('winston')
,  union = require('union');

var router = new director.http.Router();
var server = union.createServer({
  before: [
    ecstatic(__dirname + '/public')
  ]
});

router.get('/', function () {
  var self = this;
  fs.readFile('public/layout.html', 'utf-8', function(err, html) {
    [...]
  })
});

server.listen(3000, function () {
  console.log('Application is now started on port 3000');
});

How does routing with director work? When I leave ecstatic out, I can define routes like '/' and it works, but then I don't get static CSS and JS content. With ecstatic / is replaced with 'index.html' and ecstatic has priority over all defined routes. - It's the same behavior with connect-static. Route (/) is replaced by index.html.

I also tried a different approach using the connect middleware, which doesn't work:

var flatiron = require('flatiron')
,  connect = require('connect')
,  path = require('path')
,  fs = require('fs')
,  plates = require('plates')
,  app = flatiron.app;

app.use(flatiron.plugins.http);
app.use(connect.favicon());
app.use(connect.static(__dirname + '/public'));
app.use(connect.directory(__dirname + '/public'));
app.use(connect.cookieParser('my secret here'));
app.use(connect.session({'secret': 'keyboard cat'}));

app.router.get('/', function () {
  console.log("GET /");
  var self = this;
  fs.readFile('public/layout.html', 'utf-8', function(err, html) {
    [...]
  })
});

app.listen(3000, function () {
  console.log('Application is now started on port 3000');
});
like image 312
Patrick Avatar asked Mar 19 '12 08:03

Patrick


2 Answers

I think the best answer for your question about routing in flatiron is, as always, inside the source code:

     app.server = union.createServer({
           after: app.http.after,
           before: app.http.before.concat(function (req, res) {
             if (!app.router.dispatch(req, res, app.http.onError || union.errorHandler)) {
               if (!app.http.onError) res.emit('next');
             }
           }),
           headers: app.http.headers,
           limit: app.http.limit
     });

As you can see here flatiron binds router as the last request handler, that is called after all middleware. If you place 'ecstatic' in app.http.before and it will be dispatched during workflow, no other middleware would be called.

Your second block of code demonstrates that you don't undestand difference between Flatiron's .use() method from Express/Connect's. I'll try to make it clear on this example:

    flatironApp.use({
        // plugin object
        name : "pluginName"
      , attach : function(options) {  
          /*code*/  
      }
      , init : function(done) { 
          /*code*/ 
          done(); 
      }
    })

    connectApp.use(function(req, res, next) {
      /* code */
      next();
    })

If you want to use Connect's middleware in Flatiron you should place it respectively in app.http.before array like this:

    // Initiating application
    app.use(flatiron.plugins.http);

    // Adding request handlers
    app.http.before.push( connect.favicon() );
    app.http.before.push( ecstatic(__dirname + '/public') );
like image 61
Dmitry Lukichev Avatar answered Oct 13 '22 11:10

Dmitry Lukichev


var connect = require('connect');

var server = union.createServer({
  before: [
    function (req, res) {
      var found = router.dispatch(req, res);
        if (!found) {
          res.emit('next');
      }
    },
    connect.static('public')
  ]
});

I forgot to insert the dispatch-function. This works.

like image 23
Patrick Avatar answered Oct 13 '22 11:10

Patrick