npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

proxy-man

v1.0.1

Published

一个基于原生http模块实现的代理

Downloads

5

Readme

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');
});