Gathering detailed insights and metrics for koa-better-http-proxy
Gathering detailed insights and metrics for koa-better-http-proxy
Gathering detailed insights and metrics for koa-better-http-proxy
Gathering detailed insights and metrics for koa-better-http-proxy
Proxy middleware for Koa. Based on villadora/express-http-proxy
npm install koa-better-http-proxy
Typescript
Module System
Min. Node Version
Node Version
NPM Version
83.6
Supply Chain
98.7
Quality
74.9
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
119 Stars
237 Commits
42 Forks
4 Watching
8 Branches
37 Contributors
Minified
Minified + Gzipped
Latest Version
0.2.10
Package Id
koa-better-http-proxy@0.2.10
Unpacked Size
71.09 kB
Size
18.27 kB
File Count
44
NPM Version
8.3.1
Node Version
17.4.0
Cumulative downloads
Total Downloads
Last day
0%
0
Compared to previous day
Last week
0%
0
Compared to previous week
Last month
0%
0
Compared to previous month
Last year
0%
0
Compared to previous year
2
9
Koa middleware to proxy request to another host and pass response back. Based on express-http-proxy.
1$ npm install koa-better-http-proxy --save
1proxy(host, options);
To proxy URLS to the host 'www.google.com':
1var proxy = require('koa-better-http-proxy'); 2var Koa = require('koa'); 3 4var app = new Koa(); 5app.use(proxy('www.google.com'));
If you wish to proxy only specific paths, you can use a router middleware to accomplish this. See Koa routing middlewares.
Use a custom http.Agent
for proxy requests.
1var agent = new http.Agent(options); 2app.use(proxy('www.google.com', { 3 agent: agent, 4}));
The port to use for the proxied host.
1app.use(proxy('www.google.com', { 2 port: 443 3}));
Additional headers to send to the proxied host.
1app.use(proxy('www.google.com', { 2 headers: { 3 'X-Special-Header': 'true' 4 } 5}));
Headers to remove from proxy response.
1app.use(proxy('www.google.com', { 2 strippedHeaders: [ 3 'set-cookie' 4 ] 5}));
Pass the session along to the proxied request
1app.use(proxy('www.google.com', { 2 preserveReqSession: true 3}));
Provide a proxyReqPathResolver function if you'd like to operate on the path before issuing the proxy request. Use a Promise for async operations.
1app.use(proxy('localhost:12345', { 2 proxyReqPathResolver: function(ctx) { 3 return require('url').parse(ctx.url).path; 4 } 5}));
Promise form
1app.use(proxy('localhost:12345', { 2 proxyReqPathResolver: function(ctx) { 3 return new Promise(function (resolve, reject) { 4 setTimeout(function () { // do asyncness 5 resolve(fancyResults); 6 }, 200); 7 }); 8 } 9}));
The filter
option can be used to limit what requests are proxied. Return true
to execute proxy.
For example, if you only want to proxy get request:
1app.use(proxy('www.google.com', { 2 filter: function(ctx) { 3 return ctx.method === 'GET'; 4 } 5}));
You can modify the proxy's response before sending it to the client.
The intent is that this be used to modify the proxy response data only.
Note: The other arguments (proxyRes, ctx) are passed by reference, so you can currently exploit this to modify either response's headers, for instance, but this is not a reliable interface. I expect to close this exploit in a future release, while providing an additional hook for mutating the userRes before sending.
You can modify the proxy's headers before sending it to the client.
If your proxy response is gzipped, this program will automatically unzip it before passing to your function, then zip it back up before piping it to the user response. There is currently no way to short-circuit this behavior.
1app.use(proxy('www.google.com', { 2 userResDecorator: function(proxyRes, proxyResData, ctx) { 3 data = JSON.parse(proxyResData.toString('utf8')); 4 data.newProperty = 'exciting data'; 5 return JSON.stringify(data); 6 } 7}));
1app.use(proxy('httpbin.org', { 2 userResDecorator: function(proxyRes, proxyResData) { 3 return new Promise(function(resolve) { 4 proxyResData.funkyMessage = 'oi io oo ii'; 5 setTimeout(function() { 6 resolve(proxyResData); 7 }, 200); 8 }); 9 } 10}));
This sets the body size limit (default: 1mb
). If the body size is larger than the specified (or default) limit,
a 413 Request Entity Too Large
error will be returned. See bytes.js for
a list of supported formats.
1app.use(proxy('www.google.com', { 2 limit: '5mb' 3}));
You can mutate the request options before sending the proxyRequest. proxyReqOpt represents the options argument passed to the (http|https).request module.
NOTE: req.path cannot be changed via this method; use proxyReqPathResolver
instead.
1app.use(proxy('www.google.com', { 2 proxyReqOptDecorator: function(proxyReqOpts, ctx) { 3 // you can update headers 4 proxyReqOpts.headers['content-type'] = 'text/html'; 5 // you can change the method 6 proxyReqOpts.method = 'GET'; 7 return proxyReqOpts; 8 } 9}));
You can use a Promise for async style.
1app.use(proxy('www.google.com', { 2 proxyReqOptDecorator: function(proxyReqOpts, ctx) { 3 return new Promise(function(resolve, reject) { 4 proxyReqOpts.headers['content-type'] = 'text/html'; 5 resolve(proxyReqOpts); 6 }) 7 } 8}));
You can mutate the body content before sending the proxyRequest.
1app.use(proxy('www.google.com', { 2 proxyReqBodyDecorator: function(bodyContent, ctx) { 3 return bodyContent.split('').reverse().join(''); 4 } 5}));
You can use a Promise for async style.
1app.use(proxy('www.google.com', { 2 proxyReqBodyDecorator: function(proxyReq, ctx) { 3 return new Promise(function(resolve, reject) { 4 http.get('http://dev/null', function (err, res) { 5 if (err) { reject(err); } 6 resolve(res); 7 }); 8 }) 9 } 10}));
Normally, your proxy request will be made on the same protocol as the original request. If you'd like to force the proxy request to be https, use this option.
1app.use(proxy('www.google.com', { 2 https: true 3}));
You can copy the host HTTP header to the proxied express server using the preserveHostHdr
option.
1app.use(proxy('www.google.com', { 2 preserveHostHdr: true 3}));
The parseReqBody
option allows you to control parsing the request body.
For example, disabling body parsing is useful for large uploads where it would be inefficient
to hold the data in memory.
This defaults to true in order to preserve legacy behavior.
When false, no action will be taken on the body and accordingly req.body
will no longer be set.
Note that setting this to false overrides reqAsBuffer
and reqBodyEncoding
below.
1app.use(proxy('www.google.com', { 2 parseReqBody: false 3}));
Note: this is an experimental feature. ymmv
The reqAsBuffer
option allows you to ensure the req body is encoded as a Node
Buffer
when sending a proxied request. Any value for this is truthy.
This defaults to to false in order to preserve legacy behavior. Note that
the value of reqBodyEnconding
is used as the encoding when coercing strings
(and stringified JSON) to Buffer.
Ignored if parseReqBody
is set to false.
1app.use(proxy('www.google.com', { 2 reqAsBuffer: true 3}));
Encoding used to decode request body. Defaults to utf-8
.
Use null
to preserve as Buffer when proxied request body is a Buffer. (e.g image upload)
Accept any values supported by raw-body.
The same encoding is used in the userResDecorator method.
Ignored if parseReqBody
is set to false.
1app.use(proxy('httpbin.org', { 2 reqBodyEncoding: null 3}));
By default, node does not express a timeout on connections.
Use connectTimeout option to impose a specific timeout on the inital connection. (connect
for http requests and secureConnect
for https)
This is useful if there are dns, network issues, or if you are uncertain if the destination is reachable.
Timed-out requests will respond with 504 status code and a X-Timeout-Reason header.
1app.use(proxy('httpbin.org', { 2 connectTimeout: 2000 // in milliseconds, two seconds 3}));
By default, node does not express a timeout on connections.
Use timeout option to impose a specific timeout. This includes the time taken to make the connection and can be used with or without connectTimeout
.
Timed-out requests will respond with 504 status code and a X-Timeout-Reason header.
1app.use(proxy('httpbin.org', { 2 timeout: 2000 // in milliseconds, two seconds 3}));
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/18 approved changesets -- score normalized to 3
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
36 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More