DoraCMS

您现在的位置是:首页>文档内容页

文档详情

第五天——范式路由之泛式路由分析

doramart 2015-10-05 09:29:16 NodeJs226265
要想把泛式路由的特性,加入到系统中,第一个想到的是修改App.js文件的两个方法

泛式路由分析

要想把泛式路由的特性,加入到系统中,第一个想到的是修改App.js文件的两个方法:

App.prototype.get = function(route,handle){
    this._route_get_handles[route] = handle;
}

App.prototype.post = function(route,handle){
    this._route_post_handles[route] = handle;
}

上周末加入了简单的路由功能,对GET/POST进行了分离处理,也就是说 app.get("/abc") 和 app.post("/abc") 是不同的,stuwebfk框架会根据get或post方法智能判断路由到的处理器。那么,即使是泛式,也必然会分两个仓库对象,保存路由和其处理函数。还是使用原有的 this._route_get_handlesthis._route_post_handles 不过,以前是map格式,现在要修改成数组对象。

修改后的代码是:

App.prototype.get = function(route,handle){
    this._route_get_handles.push({route:route,handle:handle})
}

App.prototype.post = function(route,handle){
    this._route_post_handles.push({route:route,handle:handle})
}

当有客户请求时,会判断是GET还是POST方法,然后循环this._route_get_handles或this._route_post_handles的数组,找到匹配的路由,然后调用该路由的处理器handle即可,找不到就发送 404 。

分析就到这里,下节实际编码,并实现泛式路由功能。

文章评论

取消回复
登录 参与评论

评论列表(