A http(s) proxy middleware
Non-transparent http(s) proxy connect middleware with the ability to rewrite location headers and cookies.
Does:
- Adds
X-Forwarded-For
header entry. - Copes with connection errors and timeouts.
- URL rewriting in HTML pages
- Rewrites Location headers
- Rewrites Cookie Domains and Paths (follows nginx.org approach)
- Rewrites Referer request headers
Why another proxy middleware? Check-out:
- proxy-middleware
- http-proxy-middleware
- mod-proxy
- Others?
- You choose...
For all possible options
related to http/https see
parameter | type | description |
---|---|---|
[url] |
String | optional: base url to proxy |
[options] |
Object | optional: http, https options |
options.method |
String | upercase HTTP method |
options.protocol |
String | http: or https: see url.parse() |
options.hostname |
String | hostname |
options.port |
String | port |
options.path |
String | path |
[options.timeout=5000] |
Number | optional: timeout in (ms) default=5000 |
[option.onResponse] |
Function | optional: function (clientRes, res) allows to change statuscode and/or headers |
[option.baseUrl] |
String | optional: baseUrl of routed request, comes usually from express |
[options.cookieDomains] |
Array | optional: see example |
[options.cookiePaths] |
Array | optional: see example |
[options.preserveHost=false] |
Boolean | optional: if true request host header is preserved |
[option.isForwarded=false] |
Boolean | optional: request was forwarded from other server which set x-forwarded-host and x-forwarded-proto headers |
[option.noHtmlRewrite=false] |
Boolean | optional: Do not rewrite html/ xml responses |
With Url
const proxy = require('proxyy')
const app = require('express')()
app.use('/api', proxy('https://server.my')
app.listen(3000)
//> proxies 'http://localhost:3000/api/path' to 'https://server.my/path'
With options and legacy server
const http = require('http')
http.createServer(proxy({
baseUrl: '/proxied', // if using `express` 'baseUrl' is handled via express routing
// so no need to set this with `express`
protocol: 'http:',
host: 'server.my',
port: '4000',
path: '/api',
timeout: 5000
})).listen(3000)
//> proxies 'http://localhost:3000/proxied/path' to 'http://server.my:4000/api/path'
Rewriting Cookies
// proxy DNS is 'proxy.my'
app.use('/api', proxy(
'https://api.server.my/path', {
cookieDomains: [
['www.server.my', 'www.proxy.my'], // replace string by string
[/^(\w+)\.server.com/, '$1.proxy.my'] // replace using regex
],
cookiePaths: [
['/path', '/'], // replace string by string
[/^(\w+)\/path(\/\w+)/, '$1$2'] // replace using regex
]
}
))
//> Domain=www.server.my; --> Domain=www.proxy.my;
//> Domain=api.server.com; --> Domain=api.proxy.my;
//> Path=/path; --> Path=/;
//> Path=/a/path/doc; --> Path=/a/doc;
Requires nodejs.
$ npm install proxyy --save
$ npm test