get

  1. 1、请求的数据放在url中,对用户来说是可见的 (可见性)
  2. 2get方式不是很安全 (安全性)
  3. 3get对传参的大小有限制 1MB

post

  1. 场景:登陆注册的时候,我们使用post方式
  2. 1、数据对用户来说是不见的,请求的参数没有放在url地址栏中 (可见性)
  3. 2、相对较安全
  4. 3、对传输的数据理论上来说没有限制

一、url

  1. url地址栏中只能发送get请求

二、获取post提交的数据

  1. yarn add koa-body
const Koa = require('koa');
const koaBody = require('koa-body');

const app = new Koa();

app.use(koaBody());
app.use(ctx => {
    console.log(ctx.request.body)
});

app.listen(3000);

三、例子

//index.js
const koa = require("koa");
const router  = require("koa-router")();
const app =  new koa();
const koaBody = require("koa-body");
router.get("/search",async ctx=>{
    console.log(ctx.query)
    ctx.body = {
        code:200,
        msg:"search"
    }
})
router.post("/login",async ctx=>{
    console.log(ctx.request.body);
    ctx.body = {
        code:200,
        msg:"登陆"
    }
})
app.use(koaBody());
app.use(router.routes());
app.listen(4000)
//html- search.html
<form action="http://localhost:4000/search"
     method="get">
     <!-- name属性是为了方便前端传值,后端取值 -->
        用户名<input type="text" name="username"> <br>
        密码 <input type="text" name="pwd"> <br>
        <input type="submit">
</form>
//html- login.html
<form action="http://localhost:4000/login"
     method="post">
     <!-- name属性是为了方便前端传值,后端取值 -->
        用户名<input type="text" name="username"> <br>
        密码 <input type="text" name="pwd"> <br>
        <input type="submit">
</form>