introduce

Koa is a new Web framework, built by the same people behind Express, that aims to be a smaller, more expressive, and more robust cornerstone of web application and API development. By making use of async functions, Koa helps you discard callback functions and greatly enhances error handling. Koa does not bundle any middleware, but rather provides an elegant way to help you write server-side applications quickly and happily.

Differences between Koa and native HTTP

The advantage of Koa is that it significantly improves development efficiency, and the concept of middleware is evident in Koa. Middleware includes Listen,use, CTX, and Next. Why can Koa improve efficiency? Let’s analyze its source code.

Source code analysis

Middleware listen

The Listen middleware is a syntax candy, but it’s still HTTP Listen inside, just a layer of packaging.

listen(... args){ let server=http.createServer(this.handleRequest.bind(this)); server.listen(... args); }Copy the code

The middleware use

When we use use, we find that the function executes immediately, indicating that the use source code has a callback function to execute immediately.

Use (fn){//use passes the callback this.fn=fn; // Execute the callback after listen}Copy the code

Middleware CTX

First of all, we know that when we use KOA, we use CTX calls for both the request operation and the response operation. This is also the advantage of the KOA framework, which makes the code simpler. Now let’s explore, how does CTX work?

Create a context.js file

// let proto ={// url:request.url} function defineGetter(prop,name){// let proto ={// url:request.url} function defineGetter(prop,name){// let proto ={// url:request proto.__defineGetter__(name,function(){ return this[prop][name] //this == ctx }) } defineGetter('request','url') // url==request.url defineGetter('request','path') module.exports = protoCopy the code

In the application. In the js

CreateContext (req,res){// Build CTX const CTX = object.create (this.context)// Cross-assign CTX to three const attributes request=ctx.request=Object.create(this.request) const response=ctx.response=Object.create(this.response) ctx.req=request.req=response.req=req ctx.res=request.res=response.res=res request.ctx=response.ctx=ctx request.response=response response.request=request return ctx }Copy the code

request.js

// let url=require('url') let request ={// ctx.url == ctx.request.url get url(){ Include arguments console.log(this); Parse (this.req.url). Pathname}} module. Exports =requestCopy the code

Encapsulates the Koa module application.js

let EventEmitter=require('events') let http=require('http') let context=require('./context') let Request =require('./request') let response=require('./response') class Koa extends EventEmitter{ Constructor (){// Inherit super() from all attribute constructors; // superclass this.fn; this.context=context; this.request=request; this.response=response; } use(fn){//use passes the callback this.fn=fn; } createContext(req,res){// Build CTX const CTX = object.create (this.context)// Make CTX cross const with three attributes request=ctx.request=Object.create(this.request) const response=ctx.response=Object.create(this.response) ctx.req=request.req=response.req=req ctx.res=request.res=response.res=res request.ctx=response.ctx=ctx request.response=response response.request=request return ctx } handleRequest(req,res){ let CTX = this.createcontext (req,res) this.fn(CTX).end(ctx.body)} listen(... args){ let server=http.createServer(this.handleRequest.bind(this)); server.listen(... args); } } module.exports=Koa;Copy the code

conclusion

Although this article does not analyze all the source code, we know the implementation of middleware Listen,use and CTX, which is also the first step to understand Koa. If this article makes you interested in Koa source code, click jump Koa source code, is currently a junior student, beginning to learn front-end, if there is a wrong place, welcome to correct, thank you!