通过ts的特性,不断溯源app对象

  1. import express from 'express';
  2. const app = express();

ctrl+点击 express()
image.png

core.Express

ctrl+点击 core.Express
image.png

Application

ctrl+点击 Application

  1. export interface Application extends EventEmitter, IRouter, Express.Application {
  2. (req: Request | http.IncomingMessage, res: Response | http.ServerResponse): any;
  3. init(): void;
  4. defaultConfiguration(): void;
  5. engine(ext: string, fn: (path: string, options: object, callback: (e: any, rendered?: string) => void) => void): this;
  6. set(setting: string, val: any): this;
  7. get: ((name: string) => any) & IRouterMatcher<this>;
  8. param(name: string | string[], handler: RequestParamHandler): this;
  9. param(callback: (name: string, matcher: RegExp) => RequestParamHandler): this;
  10. path(): string;
  11. enabled(setting: string): boolean;
  12. disabled(setting: string): boolean;
  13. /** Enable `setting`. */
  14. enable(setting: string): this;
  15. /** Disable `setting`. */
  16. disable(setting: string): this;
  17. render(name: string, options?: object, callback?: (err: Error, html: string) => void): void;
  18. render(name: string, callback: (err: Error, html: string) => void): void;
  19. listen(port: number, hostname: string, backlog: number, callback?: () => void): http.Server;
  20. listen(port: number, hostname: string, callback?: () => void): http.Server;
  21. listen(port: number, callback?: () => void): http.Server;
  22. listen(callback?: () => void): http.Server;
  23. listen(path: string, callback?: () => void): http.Server;
  24. listen(handle: any, listeningListener?: () => void): http.Server;
  25. router: string;
  26. settings: any;
  27. resource: any;
  28. map: any;
  29. locals: Record<string, any>;
  30. routes: any;
  31. _router: any;
  32. use: ApplicationRequestHandler<this>;
  33. on: (event: string, callback: (parent: Application) => void) => this;
  34. mountpath: string | string[];
  35. }
  36. export interface Express extends Application {
  37. request: Request;
  38. response: Response;
  39. }

可以看到有init,set,get,listen等方法。 目前用到了get和listen

EventEmitter

而且这个Application是继承自EventEmitter和IRouter的
EventEmitter是说明它是一个流 可以执行事件监听

IRouter

Ctrl 点击IRouter看看他是什么东西
它里面有很多http的动词
image.png

小结

以上就是app的继承关系

  • Express extends Application
  • Application extends EventEmitter , IRouter…
  • 其中IRouter包含了get/post/put等方法

基本上可以看到app的全貌了,ts就跟文档一样。