亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問express這段源碼大致是什么意思,app上面的handle方法,init方法哪里來的呀

請問express這段源碼大致是什么意思,app上面的handle方法,init方法哪里來的呀

九州編程 2019-04-21 20:14:40
'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給你
                            
查看完整回答
反對 回復 2019-04-21
?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

注意看代碼里的:
mixin(app,proto,false);
意思是說把proto里的所有屬性和方法傳給app,換句話說,proto有的東西,現在app里也有了。然后注意:
varproto=require('./application');
其實application文件里就有init方法和handle方法。
                            
查看完整回答
反對 回復 2019-04-21
  • 2 回答
  • 0 關注
  • 392 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號