1, the entrance
const Koa = require('koa');
const app = new Koa();
Copy the code
When require is looking for a third-party module, it will look for the main field of package.json file under the module, and check the package.json file under the KOA warehouse directory. It can be seen that the outlet exposed by the module is the application.js file under the lib directory
So when koa is referenced in app.js, the variable koa refers to the Application class
2. Then we will use use to mount the middleware function on the app and listen to listen for the response
There are two things that need to be clarified:
1. App. use is used to mount middleware. What does it do?
2. App. listen listens to ports. What does it do?
In the use method, we first determine whether the argument is a function, otherwise we throw an error; Determine whether it is a generator function, give log to indicate that the generator usage has been deprecated, and convert through the convert function
Find the convert function as follows
Generator+Promise+Co
3. Middleware execution
const fn = compose(this.middleware);
Copy the code