2012-01-28 73 views
1

我开始使用flatiron作为Web应用程序的工具集来设置事物。Flatiron js - 导演 - 如何从表中进行异步路由?

我使用app.plugins.http导演,似乎无法弄清楚如何为静态文件创建“catchall”路线& 404s - 看来.get("<RegEx>")只与第一个文件夹位置匹配,所以如果<RegEx>/.*,它将匹配/foo,但不匹配/foo/bar

这里是我的代码,一个更好的例子:

routes.js

var routes = { 
    /* home 
    * This is the main route, hit by queries to "/" 
    */ 
    "/" : { 
    get: function(){ 
     getStatic("html/index.html",_.bind(function(err,content){ 
     if(err) throw err; 
     renderContent(this,content); 
     },this)); 
    } 
    }, 
    /* static files 
    * Last rule, if no other routes are hit, it's either a static resource 
    * or a 404. Check for the file then return 404 if it doesn't exist. 
    */ 
    '/(.*)' : { 
    get : function(){ 
     getStatic(this.req.url,_.bind(function(err,content){ 
     if(!err){ 
      renderContent(this,content); 
     } else { 
      this.res.writeHead(404); 
      // TODO: fancier 404 page (blank currently) 
      this.res.end(); 
     } 
     },this)) 
    } 
    } 
} 

,并在我的主要的应用程序文件:

/* Define the routes this app will respond to. */ 
var routes = require('./lib/routes'); 
/* set up app to use the flatiron http plugin */ 
app.use(flatiron.plugins.http); 
/* loop through routes and add ad-hoc routes for each one */ 
for(var r in routes){ 
    var route = routes[r]; 
    if(!routes.hasOwnProperty(r)) continue; 
    for(var method in route){ 
     if(!route.hasOwnProperty(method)) continue; 
     app.router[method](r,route[method]); 
    } 
} 
/* Start the server */ 
app.listen(8080); 

我希望能够保持我的路线在一个单独的模块中,并导入它们 - 我很不清楚这种方法或使用导演和香草http服务器会更好,但我已经尝试了两种方式,没有任何运气。

这里就是我得到:

localhost:8080/ 
>> (content of index file - this works) 
localhost:8080/foo 
>> (blank page, 404 header) 
localhost:8080/foo/bar 
>> (no static file for this - I get a 404 header, but the body is now "undefined" - where is this coming from??) 
localhost:8080/css/min.css 
>> (this file should exist, but the route is never called. I do however still get a 404 header, and get the "undefined" body) 

所以,我假设了“不确定”身体是不确定的路由默认行为。

有没有办法在不为每个深度添加规则的情况下创建一个catchall路线?

回答

2

尝试使用的onError:

app.use(flatiron.plugins.http,{ 
    onError: function (err) { 
     this.res.end('Nope'); 
    } 
}); 

要管理你的静态文件,我建议你用熨斗/工会+ connect.static