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

@alanchenchen/httpproxyer

v0.1.0

Published

A simple http server proxy module implented with nodejs

Downloads

6

Readme

httpProxyer

代理转发http和https请求的node包,无依赖

Author : Alan Chen

version: 0.0.9

node >= 8.11.1

Date: 2019/4/25

Build Status LICENSE

Features

  1. 支持代理http和https请求,支持自启一个代理服务器。自启的服务器只支持http的protocol
  2. 支持反向代理实现负载均衡
  3. 提供事件钩子函数拦截并重写数据
  4. 提供express的中间件
  5. 提供静态文件服务器工具

Usage

  1. npm install --save @alanchenchen/httpproxyer or yarn add @alanchenchen/httpproxyer
  2. npm包内置两个原生node编写的插件,和两个实现了express中间件的插件。
  3. npm包默认导出一个对象,包含4个key,分别是:
    • httpProxyer
    • staticServer
    • proxyMiddleware
    • staticMiddleware

Options

httpProxyer

httpProxyer对象方法

导出一个对象,自带2个方法,每个方法调用一次都会返回一个ProxyHttp实例。

  1. createProxyServer 启用一个服务器事件监听器,必须调用listen方法才监听端口并代理转发。常用于正向代理。参数如下:
    • opts [Object]
      • target [String]。必须是代理服务器的http或https地址,例如:http://127.0.0.1:7070
      • inherit [String]。可选,转发请求是否继承当前target的query和hash信息
  2. proxy 代理转发已有服务器的请求,可以实现反向代理和负载均衡。参数如下:
    • IncomingMessage [可读流],Http Server类request事件的第一个参数req
    • ServerResponse [可写流],Http Server类request事件的第二个参数res
    • opts [Object]
      • target [String]。必须是代理服务器的http或https地址,例如:http://127.0.0.1:7070
      • inherit [String]。可选,转发请求是否继承当前target的query和hash信息

ProxyHttp实例

  1. ProxyHttp实例支持事件监听,通过on(event, callback)来调用,第一个参数是事件名,第二个参数是回调函数。目前支持3个事件钩子:
    • proxyRequest 在转发请求之前触发,函数有2个参数
      • proxyReq [可写流],代理服务器请求目标服务器的数据,当调用proxyReq.write()方法或proxyReq.end()方法或pipe(proxyReq)会重写请求数据。否则默认转发客户端请求
      • opts [Object],包含请求头和请求url之类的信息对象,只读
    • proxyResponse 在转发请求,目标服务器数据响应成功,返回响应数据到客户端之前触发,函数有3个参数
      • proxyRes [可写流],代理服务器返回给源请求的数据,当调用proxyRes.write()方法或proxyRes.end()方法或pipe(proxyRes)会重写返回数据。否则默认返回目标服务器响应数据
      • res [可读流],目标服务器返回给代理服务器的数据
      • opts [Object],包含响应头和http状态码的信息对象,只读
    • proxyError 在代理服务器接收客户端请求或转发请求发生错误时触发,函数有2个参数
      • error [Error] 错误对象
      • from [String] server或client其中一字符串。server表示错误发生在代理服务请求出错。client表示代理服务器接收客户端请求出错。
  2. ProxyHttp实例自带一个listen方法,使用方法和node的http模块类似。
  3. ProxyHttp实例自带一个close方法,使用方法和node的http模块类似。可选一个回调函数,当关闭服务器后触发。

staticServer

导出一个类,自带1个静态方法start。返回一个promise,then表示当前路径是否存在静态文件,会返回文件,reject表示当前路径不存在静态文件,会返回一个Error对象。参数如下:

  • IncomingMessage [可读流],Http Server类request事件的第一个参数req
  • ServerResponse [可写流],Http Server类request事件的第二个参数res
  • opts [Object], 目前支持两个key。
    • rootPath [String] 指定文件目录作为服务器根目录,默认为'/',即进程运行的的目录
    • homePage [String] 当req的url为'/'时跳转的首页文件,默认为'index.html'

基于两个插件实现的express中间件proxyMiddlewarestaticMiddleware用法同上面类似,可以去example/express看详细例子。

Unit tests

  • test目录里目前只有一个测试用例,分别测试了httpProxyercreateProxyServer()proxy()on()方法。
  • 测试框架为mocha,如果需要增加测试用例,操作如下:
    1. git clone [email protected]:alanchenchen/httpProxyer.git
    2. 在test目录里新增测试文件,约定测试文件必须是*.test.js后缀格式,必须在js后缀前加test后缀。
    3. yarnnpm install安装开发依赖mocha
    4. npm test在终端terminal查看测试结果

Attentions

  1. httpProxyer中如果输入target,代理转发时会取URL的protocol、hostname、port和path(包括query不包括hash),当protocol为https时,port会被强制重写为443。当inherit为false,只取pathname(不包括query不包括hash)
  2. staticServer的rootPath合成的绝对路径会加上当前进程的绝对路径

license

  • Anti 996(996.ICU)