路由
路由,本质上是给路由器中注册一个’method’-‘pathname’-‘callback’对应的队列,等待请求时服务器响应执行这些队列,当响应时,在route内部判断是哪个pathname,执行对应的处理和callback回调。
- get方法是进行一个注册,
- route是createServer的回调。
路由器类似这样一个容器:
var G=this; /*全局变量*///处理get和post请求this._get={};this._post={};
注册过程 ```javascript http.createServer(app);
route.get(‘example/index?id=1’, (req, res) => { res.send(); });
route.get = function (pathname, callback) { G[pathname] = callback; // this._get[pathname] = callback; // this._post[pathname] = callback; }
- 执行过程```javascriptif(G[pathname]){G[pathname](req,res); /*执行注册的方法*/}else{res.end('no router');}
