proxy-man

1.0.1 • Public • Published

proxy-man

Build Status Coverage Status

简介

proxy-man是一个基于原生http模块实现的代理,它可以用来代理http请求,并在代理请求将要发出时,和将要接受目标响应时提供事件钩子,用来做自定义的修改。

安装

直接通过npm:

npm install proxy-man --save

例子

//代理功能
var http = require('http');
var ProxyMan = require('proxy-man');
 
var proxy = new ProxyMan();
 
http.createServer(function(req, res) {
  proxy.createProxy('http://localhost:9091', req, res);
}).listen(8081);
 
http.createServer(function(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  res.write(JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9091);
//简单的代理服务器
var http = require('http');
var ProxyMan = require('proxy-man');
 
var proxy = new ProxyMan();
proxy.createProxy('http://localhost:9091').listen(8081);
 
//请求即将发出前触发
proxy.on('beforeReqSend', function(req) {
  req.setHeader('X-Special-Proxy-Header', 'foo');
});
//响应即将返回前触发
proxy.on('beforeResGet', function(res) {
  res.writeHead(500, {
    'Content-Type': 'text/plain'
  });
  res.end('always 500, haha');
});
 
http.createServer(function(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  res.write(JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9091);
//简单负载均衡
var http = require('http');
var ProxyMan = require('proxy-man');
 
var proxy = new ProxyMan();
var address = ['http://localhost:9090', 'http://localhost:9091', 'http://localhost:9092'];
 
http.createServer(function(req, res) {
  var target = address.shift();
  proxy.createProxy(target, req, res);
  address.push(target);
}).listen(8081);

API

new ProxyMan()

var proxy = new ProxyMan();

返回一个独立的ProxyMan实例

createProxy(targetUrl, [req, res])

若参数中含有req以及res,则作为服务器内部的代理服务,将请求发往targetUrl

http.createServer(function(req, res) {
  proxy.createProxy('http://localhost:9091', req, res);
}).listen(8081);

若参数中没有req以及res,则作为独立的代理服务器,需要继续调用listen()方法监听指定端口

proxy.createProxy('http://localhost:9091').listen(8081);

listen(port)

仅作为代理服务器使用时有效,监听指定端口

close()

仅作为代理服务器使用时有效,关闭此代理服务器

event: beforeReqSend

function (request) { }

代理请求即将发出前触发,request即为将要发出的请求,可以在此事件内对其做出修改,并提供便捷方法setHeader(name, value)

proxy.on('beforeReqSend', function(req) {
  req.setHeader('X-Special-Proxy-Header', 'foo');
});

event: beforeResGet

function (response) { }

响应即将返回前触发,response即为将要返回的响应,可以在此事件内对其做出修改,response对象提供了便捷属性body来修改响应体,支持直接在事件中返回响应

proxy.on('beforeResGet', function(res) {
  res.setHeader('content-type', 'text/plain');
  res.body = 'new response body'
});
//or
proxy.on('beforeResGet', function(res) {
  res.writeHead(500, {
    'Content-Type': 'text/plain'
  });
  res.end('always 500, haha');
});

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.1
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.1
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i proxy-man

Weekly Downloads

0

Version

1.0.1

License

MIT

Last publish

Collaborators

  • qqqppp9998