前端缓存http请求

2021-03-07 15:29

阅读:334

标签:允许   str   没有   pad   add   att   data   you   二次   

需求:
 1、 重复的请求,使用缓存
 2、 不重复的请求,允许发送
 3、 连续两次重复的发送,两次返回的结果是一样的,且第二次不发送请求
 
1、搭建前端服务
vue-cli 一步到位
2、后端服务,使用koa,也是一步到位搭建
const Koa = require(‘koa‘)
const app = new Koa()
app.use(async (ctx, next)=> {
  ctx.set(‘Access-Control-Allow-Origin‘, ‘*‘);
  ctx.set(‘Access-Control-Allow-Headers‘, ‘Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild‘);
  ctx.set(‘Access-Control-Allow-Methods‘, ‘PUT, POST, GET, DELETE, OPTIONS‘);
  if (ctx.method == ‘OPTIONS‘) {
    ctx.body = 200; 
  } else {
    await next();
  }
});
app.use(async(ctx)=>{
    let url =ctx.url
    //从request中获取GET请求
    let request =ctx.request
    let req_query = request.query
    let req_querystring = request.querystring
    //从上下文中直接获取
    let ctx_query = ctx.query
    let ctx_querystring = ctx.querystring
    ctx.body={
        url,
    }
})
app.listen(3000,()=>{
    console.log(‘server is starting at port 3000‘);
})
const Koa = require(‘koa‘)
const app = new Koa()
app.use(async (ctx, next)=> {
  ctx.set(‘Access-Control-Allow-Origin‘, ‘*‘);
  ctx.set(‘Access-Control-Allow-Headers‘, ‘Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild‘);
  ctx.set(‘Access-Control-Allow-Methods‘, ‘PUT, POST, GET, DELETE, OPTIONS‘);
  if (ctx.method == ‘OPTIONS‘) {
    ctx.body = 200; 
  } else {
    await next();
  }
});
app.use(async(ctx)=>{
    let url =ctx.url
    //从request中获取GET请求
    let request =ctx.request
    let req_query = request.query
    let req_querystring = request.querystring
    //从上下文中直接获取
    let ctx_query = ctx.query
    let ctx_querystring = ctx.querystring
    ctx.body={
        url,
    }
})
app.listen(3000,()=>{
    console.log(‘server is starting at port 3000‘);
})

 



评论


亲,登录后才可以留言!