Middleware commonly used in Koa:
- Koa-session: a session that allows stateless HTTP to have state, and a cookie based implementation to store information in the background
- Koa-mysql: encapsulates SQL statements that need to be used
- Koa-mysql-session: used when you do not want the session to be stored in memory but want the session to be stored in the mysql database
- Koa-router: The background receives the REQUESTED URL, and the route uses different processing logic based on the URL.
- Koa-view: When an HTML page is requested, the background uses the template engine to render the data to the template and then returns it to the background
- Koa-static: To request img, JS, and CSS files, no other logic is required
- Koa-better-body: Parses the request body when the post uploads the file
Koa series of articles:
- The KOA framework can be used and written — (koA base framework)
- The KOA framework will use and write — (koa-router)
- The KOA framework will use and write — (koa-view, koa-static)
- – KoA frameworks will be used and written — (koA-bodyParser, KoA-better-Body)
Koa – the use of the router
var Koa = require('koa');
var Router = require('koa-router');
var app = new Koa();
var router = new Router();
router.get('/home',(ctx,next)=>{
ctx.body = 'home'
next();
});
router.get('/user', (ctx, next) => {
ctx.body = 'user';
next();
});
app.use(router.routes())
app.use(router.allowedMethods());
Copy the code
Koa – the secrets of the router
Suppose there is no KOA-Router
var Koa = require('koa'); var Router = require('koa-router'); var app = new Koa(); var router = new Router(); / / the handling of the routing to the middleware app. Use ((CTX, next) = > {the if (CTX) path = = = '/' && CTX) method = = = 'GET') {CTX. Body = 'front page'} else {next (); }}) app. Use ((CTX, next) = > {the if (CTX) path = = = '/ user' && CTX) method = = = 'GET') {CTX. Body = 'users'} else {next (); }});Copy the code
As can be seen from the above, if there is no KOA-Router, in fact, each route is processed in the form of KOA-registered middleware, which is not conducive to loose coupling and modularization. Therefore, the processing logic of all routes is extracted and combined into a large middleware KOA-Router for processing. Finally, the large middleware is registered to KOA. If you are not familiar with the principles of KOA middleware, you can refer to another article in which the KOA framework is used and written.
The principle of koa – the router
Since koA-Router is also a large middleware with many small middleware, it must also use the Onion model. The characteristics of the Onion model:
- Middles: A container used to hold registered middleware
- Get (path,fn) : used to register middleware and store it in middles
- Compose () : Used to compose middleware so that the middleware executes sequentially
- Routes () : registers the KOa-Router middleware with the APP middleware. It calls the routing middleware to match the requested path CTx. path
If you have questions about middleware and the Onion model, refer to the KOA framework which uses and writes – (KOA infrastructure)
Layer: Stores the registered routing information
Class Layer {constructor(path,callback) {this.path = path this.Copy the code
Middles: A container used to hold registered middleware
class Router {
constructor() {
this.middles = [];
}
}
module.exports = Router
Copy the code
Get: used to register middleware, stored in middles, since it is routing middleware there is an extra parameter path
class Router { constructor() { this.middles = []; } //set,post etc. Get (path,callback) {const layer = new layer (path,callback) this.middles.push(layer)}} module.exports = RouterCopy the code
Routes: registers the KOa-Router middleware with the APP middleware. The function is to invoke the routing middleware to match the request path CTx. path
class Router { constructor(){ this.middles=[]; } // get(path,fn){const layer = new layer (path,callback) this.middles.push(layer)} compose(){} routes() { // Next is the middleware registered with app.use after koa-router return async (CTX, Next) => {// request path let path = ctx.path; Let arr = [] for(let item of this.middles) {if(item.path === path) {arr.push(item.cb)}} // This.pose (CTX, ARR,next); this.pose (CTX, ARR,next); } } } module.exports = RouterCopy the code
Compose: Used to compose middleware so that the middleware executes sequentially
class Router { constructor() { this.middles = []; } // Set,post, etc. Get (path,callback){const layer = new layer (path,callback) this.middles.push(layer) Compose (CTX, ARr,next){dispatch(index){compose(CTX, ARr,next){dispatch(index){if(index === lasts. Length) return next(); let route = arr[index]; Route (CTX,()=>{dispatch(index+1)})} dispatch(0)}, return async (CTX, next) =>{// Let path = ctx.path; Let arr = [] for(let item of this.middles) {if(item.path === 'path') {arr.push(item.cb)}} // This.pose (CTX, ARR,next); this.pose (CTX, ARR,next); } } module.exports = RouterCopy the code
On the other
The above router is a simplified version of the KOA-Router. It only implements the level 1 route of the KOA-Router, but it illustrates the core idea of the KOA-Router. The KOA-Router added use to register the level 2 route, and added a lot of other logic including redirection and so on
conclusion
The principle of KOA-Router middleware is basically introduced, and then we will learn other middleware of KAO:
- The KOA framework will use and write — (koa-view, koa-static)