an-node-proxy

1.0.2 • Public • Published

an-node-proxy

an-node-proxy is an http proxying library, supports both http and https, and also supports proxy to local data with the need of mock request

Features

  • http proxy
  • https proxy
  • mock
  • more headers, au and so on
  • improve readme
  • an tool to produce index

Installation

npm install an-node-proxy --save

Ussage

http proxy

import {createServer} from 'http'
import {createProxyServer} from 'an-node=proxy'
 
// proxy server
createProxyServer({
  target: 'http://localhost:8001'
}).listen(8000)
 
// target server
createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' })
  res.write(`request from ${req.socket.localAddress}, and headers are ${JSON.stringify(req.headers, null, 2)}`)
  res.end()
}).listen(8001)

so does https, an-node-proxy also support

middileware

import express from 'express'
import {createServer} from 'http'
import {createProxy} from 'an-node-proxy'
 
const app = new express()
const proxy = createProxy({
    target: 'http://localhost:8004'
})
 
app.use((req, res) => {
    proxy.proxy(req, res)
})
 
app.listen(8003)
 
// target server
createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' })
    res.write(`request from ${req.socket.localAddress}, and headers are ${JSON.stringify(req.headers, null, 2)}`)
    res.end()
}).listen(8004)

mock

import {createServer} from 'http'
import {createProxyServer} from 'an-node-proxy'
 
// mock server
createProxyServer({
  mock: {
      rules: [
          {
              from: '/test',
              to: './examples/mock'
          },
          {
              from: '/test/a.js',
              to: './examples/a.js'
          }
      ]
  }
}).listen(8005)

Readme

Keywords

none

Package Sidebar

Install

npm i an-node-proxy

Weekly Downloads

4

Version

1.0.2

License

ISC

Unpacked Size

20.5 kB

Total Files

11

Last publish

Collaborators

  • anc95