Express基于Node.js平台,快速、开放、极简的Web开发框架安装//应用生成工具npminstallexpress-generator-g//创建express应用包expressapp//install依赖npminstall生成成功后,会生成如下目录和文件:|---bin|---node_module|---public|---routes|---view|---app。js|---package.json接下来,我们启动程序后通过:npmstart,访问127.0.0.1:3000访问快递页面。接下来,我们将通过研究源码来讨论快速路由原理的实现。路由我们查看app.js和index.js文件:app.jsvarindex=require('./routes/index');app.use('/',index);//或app.get('/',index);routes/index.jsvarexpress=require('express');varrouter=express.Router();router.get('/',function(req,res,next){res.render('index',{title:'Express'});});可以看出express的路由大概定义了一个路由规则文件,然后通过app.use()或者app[METHOD]建立路由规则访问连接,虽然两者的结果是一样的,但是有本质的差异。下图是涉及到的主要文件:接下来我们先通过源码来看一下app.use()的实现思路。app.use我们打开node_module中的express文件夹。打开lib/application.js文件。app.use=functionuse(fn){varoffset=0;变种路径='/';//'/'的默认路径//消除歧义app.use([fn])if(typeoffn!=='function'){vararg=fn;while(Array.isArray(arg)&&arg.length!==0){arg=arg[0];}//第一个参数是路径if(typeofarg!=='function'){offset=1;路径=fn;}}varfns=flatten(slice.call(arguments,offset));if(fns.length===0){thrownewTypeError('app.use()需要中间件函数');}//设置路由器this.lazyrouter();varrouter=this._router;fns.forEach(function(fn){//非expressappif(!fn||!fn.handle||!fn.set){returnrouter.use(path,fn);}debug('.useappunder%s',path);fn.mountpath=path;fn.parent=this;//在req和res上恢复.app属性router.use(path,functionmounted_app(req,res,next){varorig=req.app;fn.handle(req,res,function(err){setPrototypeOf(req,orig.request)setPrototypeOf(res,orig.response)next(err);});});//安装了一个应用程序fn.emit('mount',this);},这);返回这个;};在看到一些使用中的代码后,我们开始判断和处理通过use挂载的路径还是一个函数,通过lazyrouter()方法实例化了router类,全局只有一个router实例对象,并且router.use()方法最终被调用接着,我们到lib/router/index.js看router.use方法的现实:proto.use=functionuse(fn){varoffset=0;变种路径='/';//'/'的默认路径//消除歧义router.use([fn])if(typeoffn!=='function'){vararg=fn;while(Array.isArray(arg)&&arg.length!==0){arg=arg[0];}//第一个参数是路径if(typeofarg!=='function'){offset=1;路径=fn;}}varcallbacks=flatten(slice.call(arguments,offset));if(callbacks.length===0){thrownewTypeError('Router.use()需要中间件函数');}for(vari=0;i
