application.js是exPRess框架的核心,也是里面包括了服務端的很多配置和邏輯代碼。這里主要說一下和路由有關的一些代碼。
app.handle = function handle(req, res, callback) { var router = this._router;//這里一開始是空的,只有使用app的http verb方法或者use方法時才會去惰性加載 // final handler var done = callback || finalhandler(req, res, { env: this.get('env'), onerror: logerror.bind(this) }); // no routes if (!router) {//這里一開始是空的,只有使用app的http verb方法或者use方法時才會去惰性加載 debug('no routes defined on app'); done(); return; } router.handle(req, res, done);};app.use = function use(fn) { var offset = 0; var path = '/'; // default path to '/' // disambiguate app.use([fn]) if (typeof fn !== 'function') { var arg = fn; while (Array.isArray(arg) && arg.length !== 0) { arg = arg[0]; } // first arg is the path if (typeof arg !== 'function') { offset = 1; path = fn; } } var fns = flatten(slice.call(arguments, offset)); if (fns.length === 0) { throw new TypeError('app.use() requires middleware functions'); } // setup router this.lazyrouter(); var router = this._router; fns.forEach(function (fn) {//每一個fn對應一個Layer,所以app.use(fn)時,無論是同時傳入多個參數還是多次使用use,每個函數或中間件都對應一個Layer // non-express app if (!fn || !fn.handle || !fn.set) {//fn是函數時 return router.use(path, fn); } //fn是路由對象route時 debug('.use app under %s', path); fn.mountpath = path; fn.parent = this; // restore .app property on req and res router.use(path, function mounted_app(req, res, next) {//app.use('/',router); var orig = req.app; fn.handle(req, res, function (err) { req.__proto__ = orig.request; res.__proto__ = orig.response; next(err); }); }); // mounted an app fn.emit('mount', this); }, this); return this;};app.route = function route(path) { this.lazyrouter(); return this._router.route(path);};app.all = function all(path) { this.lazyrouter(); var route = this._router.route(path); var args = slice.call(arguments, 1); for (var i = 0; i < methods.length; i++) { route[methods[i]].apply(route, args); } return this;};1.app.handle是服務器的邏輯入口,其實然后直接通過router.handle進入到路由的查找和處理,這個查找和處理過程在上一章里已經分析過,也就是開始對router二維數組進行查找的過程。
2.app.route函數是直接通過app來配置路由的一個快捷方式,他的本質是利用了router.route方法,這個方法會讓路由形成一個二維數組的結構。而不是一維數組。
3.app.use的本質是調用router的方法進行處理,就是把傳入的函數掛載到layer層,然后儲存在router的stack中,其中有一個特殊的情況需要處理,就是如果用戶傳入了一個router類型的路由對象的時候,這時候,如果匹配了對應的路徑時,執行的是該路由對象的handle方法,然后進入該router對象的內部處理邏輯。和下面的all方法是不一樣的。
4.app.all方法本質是利用route對象進行配置路由,邏輯是一個兩層的循環,先是method數組的循環,然后是在route中具體的http方法函數里的循環。 這會在一個route對象的stack數組中存儲大量的layer。
新聞熱點
疑難解答