zoukankan      html  css  js  c++  java
  • Koa-router 学习笔记

    记录笔者日常学习笔记,无深解内容,除了笔者无人看懂。
    koa-router

    安装

    npm install --save koa-router
    

    简单使用

    // 引入Koa模块
    const Koa = require('koa');
    // 引入Koa-router
    const Router = require('koa-router');
    
    // 实例化Koa模块
    const app = new Koa();
    // 实例化路由模块
    const router = new Router();
    
    // 配置路由
    // ctx 上下文 context,  包含了request和response等信息
    router.get('/',async (ctx)=>{
      ctx.body = '首页';  //返回数据    相当于:原生里面的res.writeHead()  res.end()
    }).get('/news',async (ctx)=>{
      ctx.body = '这是一个新闻页面';
    });
    
    // 启动路由
    app
      .use(router.routes())   /*启动路由*/
      .use(router.allowedMethods());
    /*
     * router.allowedMethods()作用: 这是官方文档的推荐用法,我们可以
     * 看到 router.allowedMethods()用在了路由匹配 router.routes()之后,所以在当所有
     * 路由中间件最后调用.此时根据 ctx.status 设置 response 响应头 
     *
     */
    
    // 监听3000端口
    app.listen(3000);
    

    使用koa-router :

    1. 路由导航
    2. get请求获取参数 (ctx.query)
    3. 动态路由及其获取参数(/product/:id ctx.params.id)

    路由导航

    //引入 koa模块
     
    var Koa=require('koa');
     
    var router = require('koa-router')();  /*引入是实例化路由** 推荐*/
     
    //实例化
    var app=new Koa();
     
    router.get('/',async (ctx)=>{
        ctx.body="首页";
     
    })
     
     
    app.use(router.routes());   /*启动路由*/
    app.use(router.allowedMethods());
    /*
     * router.allowedMethods()作用: 这是官方文档的推荐用法,我们可以
     * 看到 router.allowedMethods()用在了路由匹配 router.routes()之后,所以在当所有
     * 路由中间件最后调用.此时根据 ctx.status 设置 response 响应头 
     *
     */
    app.listen(3000);
    
    

    get请求获取参数

    /*在 koa2 中 GET 传值通过 request 接收,但是接收的方法有两种:query 和 querystring。
         query:返回的是格式化好的参数对象。
         querystring:返回的是请求字符串。*/
     
    //获取get传值
    //http://localhost:3000/newscontent?aid=123
     
    router.get('/newscontent',async (ctx)=>{
     
        //从ctx中读取get传值
     
        console.log(ctx.query);  //{ aid: '123' }       获取的是对象   用的最多的方式  **推荐
        console.log(ctx.querystring);  //aid=123&name=zhangsan      获取的是一个字符串
        console.log(ctx.url);   //获取url地址
     
        //ctx里面的request里面获取get传值
     
        console.log(ctx.request.url);
        console.log(ctx.request.query);   //{ aid: '123', name: 'zhangsan' }  对象
        console.log(ctx.request.querystring);   //aid=123&name=zhangsan
     
    })
    
    

    动态路由

    //请求方式 http://域名/product/123
    router.get('/product/:aid',async (ctx)=>{
        console.log(ctx.params); //{ aid: '123' } //获取动态路由的数据
        ctx.body='这是商品页面';
    });
    
    

    动态路由传多个值

    //请求方式 http://域名/product/123/456
    router.get('/product/:aid/:cid',async (ctx)=>{
        console.log(ctx.params); //{ aid: '123',cid:'456' } //获取动态路由的数据
        ctx.body='这是商品页面';
    });
    
    
  • 相关阅读:
    【概率】Uva 10900
    【组合数的唯一分解定理】Uva1635
    【欧拉定理】计算(a^(b^c))%1000000007
    【小小的思路】坑死自己的数学水题
    【杨氏矩阵+勾长公式】POJ 2279 Mr. Young's Picture Permutations
    【dp入门题】【跟着14练dp吧...囧】
    【补】【FZU月赛】【20150515】【待续】
    【二分查找最优解】FZU 2056 最大正方形
    【二进制】FZU 2062 Suneast & Yayamao
    【阔别许久的博】【我要开始攻数学和几何啦】【高精度取模+同余模定理,*】POJ 2365 The Embarrassed Cryptographer
  • 原文地址:https://www.cnblogs.com/jiaoshou/p/13442516.html
Copyright © 2011-2022 走看看