'usestrict';varEventEmitter=require('events').EventEmitter;varmixin=require('merge-descriptors');varproto=require('./application');varRoute=require('./router/route');varRouter=require('./router');varreq=require('./request');varres=require('./response');/***Expose`createApplication()`.*/exports=module.exports=createApplication;/***Createanexpressapplication.**@return{Function}*@apipublic*/functioncreateApplication(){varapp=function(req,res,next){app.handle(req,res,next);};mixin(app,EventEmitter.prototype,false);mixin(app,proto,false);app.request={__proto__:req,app:app};app.response={__proto__:res,app:app};app.init();returnapp;}/***Exposetheprototypes.*/exports.application=proto;exports.request=req;exports.response=res;/***Exposeconstructors.*/exports.Route=Route;exports.Router=Router;/***Exposemiddleware*/exports.query=require('./middleware/query');exports.static=require('serve-static');/***Replaceremovedmiddlewarewithanappropriateerrormessage.*/['json','urlencoded','bodyParser','compress','cookieSession','session','logger','cookieParser','favicon','responseTime','errorHandler','timeout','methodOverride','vhost','csrf','directory','limit','multipart','staticCache',].forEach(function(name){Object.defineProperty(exports,name,{get:function(){thrownewError('Mostmiddleware(like'+name+')isnolongerbundledwithExpressandmustbeinstalledseparately.Pleaseseehttps://github.com/senchalabs/connect#middleware.');},configurable:true});});
2 回答
jeck猫
TA贡献1909条经验 获得超7个赞
mixin就是继承的意思,类似jquery的extend或者es6的assign,createApplication首先是创建一个app对象(js里function也是对象),然后继承了EventEimtter(只要是继承一些on,off,trigger的与事件系统相关的方法),然后继承了proto(那些init和handle方法应该就是这里来的),最后又app.request继承了req,app.respond继承了res(__proto__是对象的原型链,具体可以参考js原型继承方面的资料),执行一下app.init进行初始化,并吧app对象return给你
慕容708150
TA贡献1831条经验 获得超4个赞
注意看代码里的:mixin(app,proto,false);意思是说把proto里的所有属性和方法传给app,换句话说,proto有的东西,现在app里也有了。然后注意:varproto=require('./application');其实application文件里就有init方法和handle方法。
添加回答
举报
0/150
提交
取消