http-proxy-middleware中间件

target目标服务器
changeOrigin是否改写原始主机为目标url
pathRewrite重写路径,value替换key


var options = {
        target: 'http://www.example.org', // 目标服务器 host
        changeOrigin: true,               // 默认false,是否需要改变原始主机头为目标URL
        ws: true,                         // 是否代理websockets
        pathRewrite: {
            '^/api/old-path' : '/api/new-path',     // 重写请求,比如我们源访问的是api/old-path,那么请求会被解析为/api/new-path
            '^/api/remove/path' : '/path'           // 同上
        },
        router: {
            // 如果请求主机 == 'dev.localhost:3000',
            // 重写目标服务器 'http://www.example.org' 为 'http://localhost:8000'
            'dev.localhost:3000' : 'http://localhost:8000'
        }
   

const http = require("http");
const url = require("url");
const { createProxyMiddleware } = require("http-proxy-middleware");

const server = http.createServer((request, response) => {
  let urlStr = request.url;
  if (/\/document/.test(urlStr)) {
    const proxy = createProxyMiddleware('/document',{
        target:'https://pcapi.vip.com',
        changeOrigin:true
    })
    proxy(request,response)
  } else {
    console.log("error");
  }
});
server.listen(8080, () => {
  console.log("listen8080");
});
// https://pcapi.vip.com/document/index.php?callback=getDocumentContent&codes=pc_bottom_return&_=1622789154783


你可能感兴趣的:(http-proxy-middleware中间件)