路由

路由,本质上是给路由器中注册一个’method’-‘pathname’-‘callback’对应的队列,等待请求时服务器响应执行这些队列,当响应时,在route内部判断是哪个pathname,执行对应的处理和callback回调。

  • get方法是进行一个注册,
  • route是createServer的回调。
  • 路由器类似这样一个容器:

    1. var G=this; /*全局变量*/
    2. //处理get和post请求
    3. this._get={};
    4. 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; }

  1. - 执行过程
  2. ```javascript
  3. if(G[pathname]){
  4. G[pathname](req,res); /*执行注册的方法*/
  5. }else{
  6. res.end('no router');
  7. }