easyproxy

基于http的反向代理

npm install easyproxy
28 downloads in the last week
53 downloads in the last month

easyproxy

logo

基于http的反向代理

特点

  • 反向代理功能
  • 支持建立在http上的所有协议(HTTP, HTTPS, WebSockets)
  • http-proxy相比,easyproxy跟后端具体服务是走本地socketPath,降低性能损耗
  • easyproxy只转发本台服务器上的服务,不跨服务器转发

安装

npm install easyproxy

使用

为方便介绍,我这里使用的后端http应用采用了connect

启动应用

var proxy = require('easyproxy');
var http = require('http');
var connet = require('connect');

var work1 = connect()
work1.use(function (req, res, next) {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('work1 is running');
});

var server1 = http.createServer(work1)
var p1 = './work1.sock'

var work2 = connect()
work2.use(function (req, res, next) {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('work2 is running');
});
var server2 = http.createServer(work2)
var p2 = './work2.sock'

var p = proxy();
p.register({appname: 'work1', host: 'www.work1.com', path: p1, prefix: '/work1'});
p.register({appname: 'work2', host: 'www.work2.com', path: p2, prefix: '/work2'});

server1.listen(p1);
server2.listen(p2);
p.listen(80);

游览器访问www.work1.com/work1 结果

work1 is running

游览器访问www.work2.com/work2 , 结果

work2 is running

注册应用

p.register({appname: 'work1', host: 'work1.com', path: p1, prefix: '/work1'});

注销应用

注销某个实例

p.unregister({appname: 'work1', host: 'work1.com', path: p1, prefix: '/work1'});

注销某个应用的全部实例

p.unregister('work1');

清空全部应用

p.clear()

扩展

请求未注册应用

var p = proxy({noHandler: function (req, res) {
  ......
}});
npm loves you