1-1 get

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

1-2 post

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

1-3 url

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

1-4 获取post提交的数据

  1. yarn add koa-body
  1. const Koa = require('koa');
  2. const koaBody = require('koa-body');
  3. const app = new Koa();
  4. app.use(koaBody());
  5. app.use(ctx => {
  6. console.log(ctx.request.body)
  7. });
  8. app.listen(3000);

示例

  1. //index.js
  2. const koa = require("koa");
  3. const router = require("koa-router")();
  4. const app = new koa();
  5. const koaBody = require("koa-body");
  6. router.get("/search",async ctx=>{
  7. console.log(ctx.query)
  8. ctx.body = {
  9. code:200,
  10. msg:"search"
  11. }
  12. })
  13. router.post("/login",async ctx=>{
  14. console.log(ctx.request.body);
  15. ctx.body = {
  16. code:200,
  17. msg:"登陆"
  18. }
  19. })
  20. app.use(koaBody());
  21. app.use(router.routes());
  22. app.listen(4000)
  1. //html- search.html
  2. <form action="http://localhost:4000/search"
  3. method="get">
  4. <!-- name属性是为了方便前端传值,后端取值 -->
  5. 用户名<input type="text" name="username"> <br>
  6. 密码 <input type="text" name="pwd"> <br>
  7. <input type="submit">
  8. </form>
  1. //html- login.html
  2. <form action="http://localhost:4000/login"
  3. method="post">
  4. <!-- name属性是为了方便前端传值,后端取值 -->
  5. 用户名<input type="text" name="username"> <br>
  6. 密码 <input type="text" name="pwd"> <br>
  7. <input type="submit">
  8. </form>