gpt4 book ai didi

node.js - Flatiron js - 总监 - 如何从表进行异步路由?

转载 作者:太空宇宙 更新时间:2023-11-03 22:56:23 25 4
gpt4 key购买 nike

我开始使用 Flatiron 作为网络应用程序的工具集进行设置。

我正在将director与app.plugins.http一起使用,并且似乎无法弄清楚如何为静态文件和404创建“包罗万象”的路由 - 看来.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);

我希望能够将我的路由保存在一个单独的模块中并导入它们 - 我很不清楚这种方法或使用director和普通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)

所以,我假设“未定义”主体是未定义路由的默认行为。

有没有一种方法可以创建一条包罗万象的路线,而无需为每个深度添加规则?

最佳答案

您可以尝试使用node-ecstatic,它是 Flatiron 的静态文件服务插件。它对我来说效果很好,您可以在以下位置找到它:

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

关于node.js - Flatiron js - 总监 - 如何从表进行异步路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9042950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com