前端专栏

跨域调试问题临时解决方案

2017-09-04  本文已影响57人  TOPro
const Koa = require("koa");
const proxy = require('koa-better-http-proxy');

const app = new Koa();

const proxy_web = proxy("http://localhost:17900",{});



const proxy_list = {
    foo:"http://foo.com",
    bar:"http://bar.com",
    noop:"http://noop.com",
    nothing:"http://nothing.com",
}

const proxy_middle_ls = {};
Object.keys(proxy_list).forEach(key=>{
  proxy_middle_ls[key] = proxy(proxy_list[key],{})
});

app.use((ctx,next)=>{
  let path = ctx.path;
  let subDomain = "";
  if(/^\/([^\/]+)/.test(path)){
      subDomain = RegExp["$1"];
      if(!proxy_list[subDomain]){
        subDomain = "";
      }
  }else{
      subDomain = ""
  }

  ctx.response.set({
    "Access-Control-Allow-Origin":"*"
  });

  if(subDomain){
    path = path.replace("/" + subDomain,"");
    ctx.path = path;
    ctx.request.path = path;
    console.log(path);
    return proxy_middle_ls[subDomain](ctx,next);
  }else{
    return proxy_web(ctx,next);
  }
})


app.listen(18000);

上一篇下一篇

猜你喜欢

热点阅读