Package Exports
- http2-proxy
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (http2-proxy) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
node-http2-proxy
A simple high performance http/2 & http/1.1 to http/1.1 spec compliant proxy helper for Node.
Features
- Proxies HTTP 2, HTTP 1.1 and WebSocket
- Simple and high performance
- Hop by hop header handling.
- Connection header handling
- Via header handling
- Forwarded header handling
Installation
$ npm install http2-proxyNotes
http2-proxy requires node v8.5.0 or newer with http2 enabled. Pass the --expose-http2 option when starting node v8.x.x.
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)Proxy HTTP/2, HTTP/1.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)
}
})
})web (req, res, options, [callback])
req:http.IncomingMessageorhttp2.Http2ServerRequestres:http.ServerResponseorhttp2.Http2ServerResponseoptions: See Optionscallback(err): Called on completion or error. Optional
Returns a promise if no callback is provided.
See request
ws (req, socket, head, options, [callback])
req:http.IncomingMessagesocket:net.Sockethead:Bufferoptions: See Optionscallback(err): Called on completion or error. Optional
Returns a promise if no callback is provided.
See upgrade
Options
hostname: Proxyhttp.request(options)target hostnameport: Proxyhttp.request(options)target portproxyTimeout: Proxyhttp.request(options)timeoutproxyName: Proxy name used for Via headertimeout:http.IncomingMessageorhttp2.Http2ServerRequesttimeoutonReq(req, options): Called before proxy requestreq:http.IncomingMessageorhttp2.Http2ServerRequestoptions: Options passed tohttp.request(options)
onRes(req, resOrSocket): Called before proxy responsereq:http.IncomingMessageorhttp2.Http2ServerRequestresOrSocket: Forwebhttp.ServerResponseorhttp2.Http2ServerResponseand forwsnet.Socket
HTTP/2 API
If HTTP/1 support is not required use the HTTP/2 stream API which has a lower overhead.
import http2 from 'http2'
import proxy from 'http2-proxy'
const server = http2.createServer()
server.listen(8000)Proxy HTTP/2
server.on('stream', (stream, headers) => {
proxy.web(stream, headers, {
hostname: 'localhost'
port: 9000
}, err => {
if (err) {
console.error('proxy error', err)
}
})
})Add x-forwarded headers
server.on('stream', (stream, headers) => {
proxy.web(stream, { headers }, {
hostname: 'localhost'
port: 9000,
onReq: (stream, headers) => {
headers['x-forwarded-for'] = stream.socket.remoteAddress
headers['x-forwarded-proto'] = stream.socket.encrypted ? 'https' : 'http'
headers['x-forwarded-host'] = stream.headers['host']
}
}, err => {
if (err) {
console.error('proxy error', err)
}
})
})web (stream, headers, options, [callback])
stream:http2.Http2Streamheaders: Request headers objectoptions: See Optionscallback(err): Called on completion or error. Optional
Returns a promise if no callback is provided.
See request
Options
hostname: Proxyhttp.request(options)target hostnameport: Proxyhttp.request(options)target portproxyTimeout: Proxyhttp.request(options)timeoutproxyName: Proxy name used for Via headertimeout:http.IncomingMessageorhttp2.Http2ServerRequesttimeoutonReq(req, options): Called before proxy requestreq:http.IncomingMessageorhttp2.Http2ServerRequestoptions: Options passed tohttp.request(options)
onRes(req, headers): Called before proxy responsereq:http.IncomingMessageorhttp2.Http2ServerRequestheaders: Response headers object