Postman
Get 获取
Post 新增
Put 修改
Delete 删除
query:
http://localhost:3000?blog=abc123
报文:
age = 23
body
header(被框架工具添加完整)
常用header
Content-Type
Content-Type是一个特殊的http header,原生获取2进制流对象,解析body的格式
- 一定要注意这个content-type和你的body是一个数据类
json数据application/jason, text文本text/plain...
Content-Type:application/jason
Content-Type:text/plain
user-agent
//navigator.userAgent
一些服务厂商涞源信息等
accept
客户端告诉服务端我只要某种类型的数据
cache-control
cache-control:在浏览器上缓存多少秒
code-
304 这份文件到这次没有进行修改不需要刷新,但还是请求了服务器确认是否修改,一定和服务器进行了交互
(from disk cache) 通过缓存打开
content-length
字节长度
优化:tracking 不传content-length,读到标记位
connection
keep-alive
保持host一段时间活性,在改时间内请求复用(for互联网路由器)
code
w3.org/Protocols/
rfc文档最有权威
- 适当看博客(时效性/理解错误)
- 尽量看rfc文档
RESTFUL API
representational state transfer
设计服务器
系统建模(用户商品库存)/抽象
增删改查
一般后面需要创建其对象,命名大写,否则小写
"use strict";
const koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router =require('koa-router');
const app = new koa();
app.use(bodyParser({
enableTypes:['json','text']
}));
// app.use(async(ctx,next)=>{
// console.log(ctx.method);
// console.log(ctx.headers);
// const {body} = ctx.request;
// //const body = ctx.request.body;
// console.log(body);
// console.log(typeof body);
// if(ctx.method === "GET"){
// ctx.text="Hello ";
// await next();
// ctx.body += "!";
// ctx.set('cache-control','max-age=5');
// }
// })
// app.use(async(ctx,next)=>{
// ctx.body=`${ctx.text},world`;
// })
const users ={//模拟数据库user表
kitty:{
name:"Kitty",
age:3
},
};
const userRouter=new Router();
userRouter.get('/user/:name',async(ctx)=>{//默认格式
const {name} = ctx.params;
if(users[name]){
ctx.body=users[name];
}
ctx.status=404;
ctx.body = {
message:`${name} not found`
}
});
userRouter.post('/user',async(ctx)=>{
ctx.body="user post";
});
userRouter.put('/user',async(ctx)=>{
ctx.body="user put";
});
userRouter.delete('/user',async(ctx)=>{
ctx.body="user delete";
});
const productRouter=new Router();
productRouter.get('/product',async(ctx)=>{
ctx.body="pro get";
});
productRouter.post('/product',async(ctx)=>{
ctx.body="pro post";
});
productRouter.put('/product',async(ctx)=>{
ctx.body="pro put";
});
productRouter.delete('/product',async(ctx)=>{
ctx.body="pro delete";
});
app.use(userRouter.routes());
app.use(productRouter.routes());
app.listen(3000);
网友评论