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