0.0.1 • Published 7 years ago

proxy-the-world v0.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
7 years ago

Proxy-The-World

可能是『最』易用的静态资源代理工具, 简单的配置,就能启动一个属于你自己的代理服务,代理从未如此简单。 支持 HTTPS 代理,支持阿里 cdn 的 combo 资源代理。代理的 url 对象符合 Node.js 的规范。

优点

  • 配置简单,只有三个配置项
  • 支持 HTTPS
  • 支持 阿里 cdn 的 combo 资源代理
  • 不需要再配置反向代理地址,本代理对没有匹配到的请求,一律会根据 url 反查 dns 的服务器地址,自动代理到线上资源

如何使用

const proxy  = require('proxy-the-world')
const path = require('path')
const fs = require('fs')

const options = {
    ssl: {
        key: fs.readFileSync('/Users/zwhu/ssl/privatekey.pem'),
        cert: fs.readFileSync('/Users/zwhu/ssl/certificate.pem')
    },
    // string || boolean || function || regexp
    from: (urlObject, headers) => {
        return true
    },
    // object || function
    target: (urlObject, headers) => {
       if (/log/.test(urlObject.pathname)) {
            return {
                protocol: 'file:', // https | http | file
                hostname: '',
                port: '',
                path: path.join(__dirname, '/build')
            }
        } else {
            return {
                protocol: 'http', // https: | http: | file:
                hostname: 'g.alicdn.com',
                port: 80,
                pathname: urlObject.pathname
            }
        }

    }
}

proxy(options)

访问下图两个链接,说明代理服务器启动成功

http

https

options 说明

PropertyDescriptionDefaultType
ssl自己生成的证书以及公私钥地址{}Object
from设置代理规则trueString | Object | Function
target满足 from 设置代理规则的 url 被转发的地址{ protocol: 'file', path: './build' }Object | Function

具体用法,请参考 examples

TODO:

  • 测试用例
  • Eslint
  • 支持命令行指定参数启动
  • 集成到开发环境中使用