1-1 get
1、请求的数据放在url中,对用户来说是可见的 (可见性)2、get方式不是很安全 (安全性)3、get对传参的大小有限制 1MB
1-2 post
场景:登陆注册的时候,我们使用post方式1、数据对用户来说是不见的,请求的参数没有放在url地址栏中 (可见性)2、相对较安全3、对传输的数据理论上来说没有限制
1-3 url
url地址栏中只能发送get请求
1-4 获取post提交的数据
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.jsconst 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>