Flatiron.js routing and combining templates, director and tablets?

Based on the expression express.js, I want to let flatiron try a small project. However, there are some minor issues that prevent me from actually getting anywhere.

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 work with a director? When I leave ecstatic , I can define routes like '/' and it works, but then I don't get static CSS and JS content. With ecstatic / it is replaced with index.html, and ecstatic takes precedence over all defined routes. - This is the same behavior with connect-static. The route (/) is replaced by index.html.

I also tried using a different approach, using middleware to connect, which does not 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'); }); 
+7
source share
2 answers

I think the best answer to 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 the router as the last request handler that is called after all the middleware. If you put "ecstatic" in app.http.before and it is sent during the workflow, no other middleware will be called.

The second block of code demonstrates that you are not using the difference between the Flatiron.use () method from Express / Connect. I will try to clarify 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 middleware in Flatiron, you must put it accordingly in the app.http.before array as follows:

  // Initiating application app.use(flatiron.plugins.http); // Adding request handlers app.http.before.push( connect.favicon() ); app.http.before.push( ecstatic(__dirname + '/public') ); 
+4
source
 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 send function. It works.

+2
source

All Articles