zoukankan      html  css  js  c++  java
  • koa 中间件

    中间件就是匹配路由之前或者匹配路由完成做的一系列的操作,我们就可以把它叫做中间件。

    在express ,  中间件(Middleware)是一个函数,它可以访问请求对象(request object (req)),  响应对象(response object (res)), 和web 应用中处理请求-响应循环流程中的中间件,一般被命名为next 的变量。在Koa 中中间件和express 有点类似。

    中间件的功能包括:
    执行任何代码。
    修改请求和响应对象。
    终结请求-响应循环。
    调用堆栈中的下一个中间件。

    应用级中间件:

    const Koa = require('koa')
    const app = new Koa()
    const router = require('koa-router')()
    
    //应用级中间件,匹配任何路由,如果get,post回调函数中没有next参数,这个路由被匹配到了就不会继续向下匹配
    app.use(async (ctx)=>{
        console.log('应用级中间件')
    })
    
    router.get('/', async (ctx) => {
        console.log("首页")
        ctx.body = '<h3>首页</h3>'
    })
    
    router.get('/register',async (ctx)=>{
        console.log("注册页")
        ctx.body = "注册页"
    })
    
    app.use(router.routes())
    app.use(router.allowedMethods())
    
    app.listen(3000)

    两个路由都是 只打印出 “应用级中间件”,页面上是 Not Found

    const Koa = require('koa')
    const app = new Koa()
    const router = require('koa-router')()
    
    //使用应用级中间件在匹配路由之前打印当前时间
    //如果需要往下匹配的话,那么需要写next()
    app.use(async (ctx,next)=>{
        console.log(new Date())
        await next() //当前路由匹配完成以后继续向下匹配
    })
    
    router.get('/', async (ctx) => {
        console.log("首页")
        ctx.body = '<h3>首页</h3>'
    })
    
    router.get('/register',async (ctx)=>{
        console.log("注册页")
        ctx.body = "注册页"
    })
    
    app.use(router.routes())
    app.use(router.allowedMethods())
    
    app.listen(3000)

    ① 匹配 首页 (注册页) 路由时,

      打印顺序:

        2020-06-19T14:11:10.891Z
        首页

        然后页面渲染 “ 首页 ”

    ② 没有匹配到 login 路由时:

        只打印出 当前时间

        页面上是 Not Found

    路由级中间件:

    const Koa = require('koa')
    const app = new Koa()
    const router = require('koa-router')()
    
    router.get('/',async (ctx)=>{
        console.log("首页1")
        ctx.body="首页1"
    })
    
    router.get('/', async (ctx) => {
        console.log("首页2")
        ctx.body = "首页2"
    })
    
    router.get('/register', async (ctx, next) => {
        console.log("注册页1")
        ctx.body = "注册页1"
        await next()
    })
    
    router.get('/register',async (ctx,next)=>{
        console.log("注册页2")
        ctx.body="注册页2"
        await next()
    })
    
    app.use(router.routes());  
    app.use(router.allowedMethods());
    
    app.listen(3000);

    匹配到 / 时,打印出 “首页1”,页面上渲染 “首页1”

    匹配到 /register 时,先后打印出 “注册页1”,“注册页2” , 页面上渲染 “注册页2”

    错误处理中间件:

    const Koa = require('koa')
    const app = new Koa()
    const router = require('koa-router')()
    
    app.use(async (ctx,next)=>{
        console.log("应用级中间件")
        next()
    
        if(ctx.status == 404) {
            ctx.status=404
            ctx.body='404 页面'
        }
        else{
            console.log(ctx.url)
        }
    })
    
    router.get('/',async (ctx)=>{
        console.log("首页")
        ctx.body='首页'
    })
    
    router.get('/login',async (ctx)=>{
        console.log('登录页')
        ctx.body="登录页"
    })
    
    app.use(router.routes());  
    app.use(router.allowedMethods());
    app.listen(3000);
    

    匹配 “ / ” 时,先后打印出 “ 应用级中间件 ” 、 “首页” 、 “ / ” ,页面上渲染 “ 首页”

    没有匹配到 "/regiser " ,只打印出 “应用级中间件 ”  , 页面上渲染 “404页面”

    中间件的执行流程:

    const Koa = require('koa')
    const app = new Koa()
    const router = require('koa-router')()
    
    app.use(async (ctx, next) => {
        console.log(1);
        await next();
        console.log(2);
    })
    
    app.use(async (ctx, next) => {
        console.log(3);
        await next();
    
        console.log(4);
    })
    
    router.get('/', async (ctx) => {
        console.log('首页');
        ctx.body = "首页";
    
    })
    router.get('/login', async (ctx) => {
    
        console.log('登录页');
        ctx.body = '登录页';
    })
    
    
    app.use(router.routes());  
    app.use(router.allowedMethods());
    
    app.listen(3000);
    

    匹配 ' / ' 时,

    没有匹配到 /register 时,

    1,3,4,2

  • 相关阅读:
    Binder机制,从Java到C (5. IBinder对象传递形式)
    Binder机制,从Java到C (4. Parcel)
    Binder机制,从Java到C (3. ServiceManager in Java)
    Binder机制,从Java到C (2. IPC in System Service :AMS)
    Binder机制,从Java到C (10. Binder驱动)
    Binder机制,从Java到C (8. ServiceManager in Native)
    Binder机制,从Java到C (7. Native Service)
    Binder机制,从Java到C (6. Binder in Native : libbinder)
    Binder机制,从Java到C (1. IPC in Application Remote Service)
    SVM详解
  • 原文地址:https://www.cnblogs.com/shanlu0000/p/13166803.html
Copyright © 2011-2022 走看看