美文网首页
koa获取get参数

koa获取get参数

作者: Wrestle_Mania | 来源:发表于2019-11-24 15:45 被阅读0次

    query

    const Koa = require("koa");
    const router = require("koa-router")();
    
    const app = new Koa();
    
    // http://localhost:8080/?a=1&b=2
    router.get("/", async ctx => {
      console.log(ctx.query); //{a:1,b:2}
      console.log(ctx.querystring); // a=1&b=2
      console.log(ctx.request.query); //{a:1,b:2}
      console.log(ctx.request.querystring); //a=1&b=2
      ctx.body = "home";
    });
    
    app
      .use(router.routes()) // 启动路由
      .use(router.allowedMethods());
    
    app.listen(8080);
    

    params

    const Koa = require("koa");
    const router = require("koa-router")();
    
    const app = new Koa();
    
    // http://localhost:8080/news/aaa
    router.get("/news/:id", async ctx => {
      console.log(ctx.params); //{id:'aaa'}
      ctx.body = "news";
    });
    
    app
      .use(router.routes()) // 启动路由
      .use(router.allowedMethods());
    
    app.listen(8080);
    

    和vue是一模一样的呀

    相关文章

      网友评论

          本文标题:koa获取get参数

          本文链接:https://www.haomeiwen.com/subject/lcvlwctx.html