通过ts的特性,不断溯源app对象
import express from 'express';const app = express();
core.Express
Application
ctrl+点击 Application
export interface Application extends EventEmitter, IRouter, Express.Application {(req: Request | http.IncomingMessage, res: Response | http.ServerResponse): any;init(): void;defaultConfiguration(): void;engine(ext: string, fn: (path: string, options: object, callback: (e: any, rendered?: string) => void) => void): this;set(setting: string, val: any): this;get: ((name: string) => any) & IRouterMatcher<this>;param(name: string | string[], handler: RequestParamHandler): this;param(callback: (name: string, matcher: RegExp) => RequestParamHandler): this;path(): string;enabled(setting: string): boolean;disabled(setting: string): boolean;/** Enable `setting`. */enable(setting: string): this;/** Disable `setting`. */disable(setting: string): this;render(name: string, options?: object, callback?: (err: Error, html: string) => void): void;render(name: string, callback: (err: Error, html: string) => void): void;listen(port: number, hostname: string, backlog: number, callback?: () => void): http.Server;listen(port: number, hostname: string, callback?: () => void): http.Server;listen(port: number, callback?: () => void): http.Server;listen(callback?: () => void): http.Server;listen(path: string, callback?: () => void): http.Server;listen(handle: any, listeningListener?: () => void): http.Server;router: string;settings: any;resource: any;map: any;locals: Record<string, any>;routes: any;_router: any;use: ApplicationRequestHandler<this>;on: (event: string, callback: (parent: Application) => void) => this;mountpath: string | string[];}export interface Express extends Application {request: Request;response: Response;}
可以看到有init,set,get,listen等方法。 目前用到了get和listen
EventEmitter
而且这个Application是继承自EventEmitter和IRouter的
EventEmitter是说明它是一个流 可以执行事件监听
IRouter
Ctrl 点击IRouter看看他是什么东西
它里面有很多http的动词
小结
以上就是app的继承关系
- Express extends Application
- Application extends EventEmitter , IRouter…
- 其中IRouter包含了get/post/put等方法
基本上可以看到app的全貌了,ts就跟文档一样。
