preface
The main content of this article is to learn koA’s source code with others by describing the author’s own process of learning koA’s source code. Koa was designed to be a smaller, more expressive, and more robust cornerstone of Web application and API development. Because of its high scalability, koA’s source code is relatively easy to read. If you follow my article to learn, it must be my bad writing.
Why read the source code
First say why to learn the source code, the main two points:
- Improve understanding of KOA and even egg (based on KOA)
- Improve your coding ability
I think the source of the learning efficiency is far higher than simple learning principle concept (just for the interview when I didn’t say), if you learned a lot of js, the principle of the concept, advanced usage but found that is rarely used in practical projects, by reading the source code can learn the great spirit in high quality open source projects is how to apply these skills, if you want to improve your programming skills, Reading the source code is a great way to do this.
The preparatory work
Preparation is relatively simple
- Download koA source code:
git clone https://github.com/koajs/koa.git
Copy the code
- Open the Node documentation: http://nodejs.cn/api/
- Start a KOA project: if not, go to https://koa.bootcss.com/
Note: THIS article will not explain koA ideas first, but rather learn them as you work through your code
The overall structure
With the source code ready, let’s take a look at the directory structure. I will describe some of the methods I use to learn KOA source code in the article, and MY understanding of it, there are mistakes, please forgive me.
Through we opened the source directory structure, but suddenly see a lot of folders and files do not know how to start?
One, don’t panic, first look at what you know of the documents
- Package. json (Basic project information)
- Readme.md (Basic Introduction)
- LICENSE, (Related to open-source agreements)
- History.md(Historical information)
- AUTHORS (Contributor information)
- .gitignore(git ignore configuration)
Two, look at some more although they may not have written but must be very familiar
- Eslintrc.yml (esLint configuration related)
- .editorConfig (Editor format convention)
So the rest we may not recognize but it does not interfere with the above information we assume, should also be some kind of configuration or information records.
Three, let’s look at the information related to the folder, or start from the know. Github has only 4 test and doc folders outside of.github. Benchmarks may be unusual, but when we Google it, we see that they are used to validate performance and benchmark data and do not significantly affect the source code we intend to read. Lib, on the other hand, is typically a called assembly in front-end engineering (in many cases SRC is compiled and generated).
Know the directory information, which file to start with
We can see that a project starts with package.json
You can get basic description information, script information, dependencies, and so on from package.json.
(If you are not familiar with package.json, please refer to the documentation for a brief study.)
When you open package.json, you’ll see that the most important information for us right now is in line 5
"main": "lib/application.js"
Copy the code
Found the entry file, application.js, in the lib directory
Lib directory
From the above analysis we know that all other directories are test or document-related, so this lib directory becomes critical.
Open the lib directory and find that the directory has no subdirectories and only four files. At this time, we will start a basic KOA project, Hello World
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
ctx.body = 'Hello World';
}); app.listen(3000); Copy the code
We also know that the entry file is lib/application.js, so we know that the Koa exported by const Koa = require(‘ Koa ‘) in the first line above refers to the application.js file.
I opened the application.js file and found that it was an uncompiled JS file (usually lib files are SRC files compiled and unreadable). Then I opened the Koa file in node_module in the Koa project (not the source project). Find that the lib files inside are exactly the same as in the source code. At this point, we can confirm that the Koa project is not compiled, and the export uses the source code, so there is no SRC directory, only the lib directory, and the content of the lib directory is the source code. And with only four files that add up to less than 2,000 lines, it’s really neat.
conclusion
This directory structure here, we first analysis the next article will tell about the application. The content of the js, this article completely according to the author himself source described the process of learning, writing is not good to read there may be a bit of laundry list, but the author will try to describe clearly, and the source of some of the techniques of reading to share, please collect the thumb up support.
Related articles
Hand in hand with you to learn Koa source code (a) – directory structure
Learn Koa source code with you hand in hand (2) – Appilication
This article is formatted using MDNICE