Flatiron的JS – 导演 – 如何从表中asynchronous路由?

我开始使用熨斗作为Web应用程序的工具集来设置事物。

我使用app.plugins.http导演,似乎无法弄清楚如何为静态文件和404s创build一个“catchall”路由 – 看起来.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) 

所以,我假设“未定义”正文是未定义路线的默认行为。

有没有一种方法来创build一个catchall路线而不添加每个深度的规则?

你可以尝试使用node-ecstatic这是一个静态文件服务附加熨斗。 它适用于我,您可以在以下位置find它:

https://github.com/colinf/node-ecstatic

尝试使用onError:

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

要pipe理你的静态文件,我build议你使用flatiron / union + connect.static