math1as
本帖最后由

本帖最后由 math1as 于 2016-6-17 14:42 编辑

同时 中间调用了路由函数,来对用户提交的pathname进行匹配,返回其想要的数据

route.js是路由函数所在地

其中用一个handle数组来保存pathname对应的控制函数。

我们新的路由定义可以在这里进行添加。

那么检测存在与否直接用typeof就行了,如果没有的话是返回Null的。

[mw_shl_code=javascript,true] var showpage=require('./showpage');

var handle={};

handle['/']=showpage.home;

handle['/home']=showpage.home;

handle['/blog']=showpage.blog;

function route(pathname) //路由函数,接受传递入的参数,并进行判断是否存在

{

if(typeof(handle[pathname])==='function')

{

return handle[pathname](); //调用处理

}

else

{

return("404 您的页面没有找到");

}

}

exports.route = route;[/mw_shl_code]

这样就大体实现了nodeJS的简单路由功能。