http协议概述
请求的常用方法
这四种请求方法仅仅只是发送了请求而已,具体的功能还是要通过具体的代码来实现
http协议的状态码
用postman请求常用方法
-
get请求
-
post请求
-
put请求
id=1,苹果香蕉鸭梨变成了苹果草莓鸭梨
-
delete请求
苹果草莓鸭梨变成了苹果鸭梨
server.js
const Koa = require("koa");
const router = require("koa-router")();
const parser = require("koa-parser");
const app = new Koa();
app.use(parser());
let dataList = ["香蕉","苹果","鸭梨"];
//get查看
router.get("/fruits", ctx =>{
ctx.body = dataList;
});
//post添加
router.post("/fruits", ctx =>{
let fruit = ctx.request.body.fruit;
dataList.push(fruit);
ctx.body = dataList;
});
//put修改
router.put("/fruits/:id", ctx =>{//路由传参:id
let id = ctx.params.id;
let fruit = ctx.request.body.fruit;
dataList.splice(id,1,fruit);
ctx.body = dataList;
});
//delete删除
router.delete("/fruits/:id", ctx =>{
let id = ctx.params.id;
dataList.splice(id,1);
ctx.body = dataList;
});
app.use(router.routes());
app.listen("3000",() => {
console.log("server is running");
});