HCanber / node-http2-proxy

A simple http/2 & http/1.1 to http/1.1 spec compliant proxy helper for Node.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

node-http2-proxy

A simple http/2 & http/1.1 to http/1.1 spec compliant proxy helper for Node.

Features

Installation

$ npm install http2-proxy

Notes

http2-proxy requires at least node v8.7.0.

HTTP/1 API

You must pass allowHTTP1: true to the http2.createServer or http2.createSecureServer factory methods.

import http2 from 'http2'
import proxy from 'http2-proxy'

const server = http2.createServer({ allowHTTP1: true })
server.listen(8000)

You can also use http-proxy2 with the old http && https API's.

import http from 'http'

const server = http.createServer()
server.listen(8000)

Proxy HTTP/2, HTTP/1 and WebSocket

server.on('request', (req, res) => {
  proxy.web(req, res, {
    hostname: 'localhost'
    port: 9000
  }, err => {
    if (err) {
      console.error('proxy error', err)
    }
  })
})
server.on('upgrade', (req, socket, head) => {
  proxy.ws(req, socket, head, {
    hostname: 'localhost'
    port: 9000
  }, err => {
    if (err) {
      console.error('proxy error', err)
    }
  })
})

Use Helmet to secure response headers

server.on('request', (req, res) => {
  proxy.web(req, res, {
    hostname: 'localhost'
    port: 9000,
    onRes: (req, res) => helmet(req, res, () => {})
  }, err => {
    if (err) {
      console.error('proxy error', err)
    }
  })
})

Add x-forwarded headers

server.on('request', (req, res) => {
  proxy.web(req, res, {
    hostname: 'localhost'
    port: 9000,
    onReq: (req, { headers }) => {
      headers['x-forwarded-for'] = req.socket.remoteAddress
      headers['x-forwarded-proto'] = req.socket.encrypted ? 'https' : 'http'
      headers['x-forwarded-host'] = req.headers['host']
    }
  }, err => {
    if (err) {
      console.error('proxy error', err)
    }
  })
})

Follow Redirects

const http = require('follow-redirects').http

server.on('request', (req, res) => {
  proxy.web(req, res, {
    hostname: 'localhost'
    port: 9000,
    onReq: (req, options) => http.request(options)
  }, err => {
    if (err) {
      console.error('proxy error', err)
    }
  })
})

web (req, res, options, [callback])

Returns a promise if no callback is provided.

See request

ws (req, socket, head, options, [callback])

Returns a promise if no callback is provided.

See upgrade

Options

License

MIT

About

A simple http/2 & http/1.1 to http/1.1 spec compliant proxy helper for Node.

License:MIT License


Languages

Language:JavaScript 100.0%