First, use middleware in your application

  1. Create a new auth2.js in the app/ Middleware folder
module.exports = (option,app) = > {

  return async function auth2(ctx,next) {

    console.log(new Date);

    // The following statement is very important. Without this statement, the thread would terminate at this point
    awaitnext(); }}Copy the code
  1. Registered middleware

Register in config.default.js.

config.middleware = ['auth2'];
Copy the code
  1. Pass parameters to the middleware
config.middleware = ['auth2'];
Copy the code
  1. The middleware reads the passed parameters
return async function auth2(ctx,next) {
    console.log(option);

    console.log(new Date);

    // The following statement is very important. Without this statement, the thread would terminate at this point
    await next();
  }
Copy the code

Using middleware in router.js

The goal of this section is to configure middleware for the specified route. This method does not need to register routes in config.default.js.

  1. Middleware is defined in the same way as above.
  2. Get the middleware in router.js and specify which routes can be triggered.
module.exports = app= > {
  const { router, controller } = app;
  const auth = app.middleware.auth();
  router.get('/',auth, controller.home.setSession);
  router.get('/news', controller.news.index);
  router.get('/deletecookie',controller.home.deletecookie)
};
Copy the code

Middleware that uses KOA in egg.js

The following is a systematic illustration of the use of koA-JSONP middleware in Egg.

  1. Installation of koa – the json
npm install koa-jsonp
Copy the code
  1. Create a new jsonp.js in app/ Middleware
// Configure the middleware for KOA

const jsonp = require('koa-jsonp');

module.exports = jsonp;
Copy the code
  1. Register middleware (in config.default.js)
config.middleware = ['jsonp'];
Copy the code
  1. Request to specify path
http:/ / 127.0.0.1:7001 / shop? callback=666
Copy the code

We continue with a piece of KOA-COMPRESS middleware that enables server Gzip compression.

  1. Installation of koa – compress
npm install koa-compress
Copy the code
  1. Create a new compress. Js under app/ Middleware
module.exports = require('koa-compress');
Copy the code
  1. Registered middleware
config.middleware = ['jsonp'.'compress'];
Copy the code
  1. Configure middleware parameters
config.compress = {
    threshold: 1024   // Set the compression threshold
}
Copy the code

The files that access the specified page are compressed.

If non-standard middleware is used, the definition of middleware is as follows:

// The following illustrates the use of non-standard middleware

const middleWare = require('xxx');

module.exports = (option,app) = > {
  return middleWare(options1,options2)
}
Copy the code

General configuration of middleware

The following three common configuration items are supported by both reference layer loaded middleware and framework reloaded middleware.

  1. Enable: controls whether the middleware is enabled.
  2. Match: Sets that only requests that match certain rules will pass through this middleware.
  3. Ignore: Sets requests that comply with certain rules not to pass through this middleware.
  • Disabling a middleware
config.compress = {
    enable: false.threshold: 10   // Set the compression threshold
  }
Copy the code
  • Sets a route to match the middleware
config.auth = {
    match: '/news'.title: 'auth hello'
  }
Copy the code

If you want to ignore a route, use the following notation

config.auth = {
    ignore: '/news'.title: 'auth hello'
  }
Copy the code

If we are dealing with a more complex routing situation, we can also use the following match method

config.auth = {
    match(ctx) {
      if (ctx.request.url === '/shop' || ctx.request.url === "/news") {
        return true;
      }
      return false
    },
    title: 'auth hello'
  }
Copy the code

Five, set the unauthorized user cannot access the specified page

  1. Create the specified middleware under app/ Middleware /
module.exports = (option,app) = > {
  return async function auth(ctx,next) {
    if (ctx.session && ctx.session.userinfo) {
      await next()
    } else {
      if (ctx.request.url === '/') {
        await next();
      } else {
        ctx.redirect('/'); }}}}Copy the code
  1. Registered middleware
  config.middleware = ['auth'.'compress'.'adminAuth'];
Copy the code
  1. Causes the specified route to trigger the middleware
  config.adminAuth = {
    match: '/admin/user'
  }
Copy the code