simple-http-proxy
Advanced tools
Comparing version
35
index.js
@@ -6,8 +6,8 @@ | ||
var url = require('url') | ||
, debug = require('debug')('simple-http-proxy') | ||
, protocols = { | ||
http: require('http'), | ||
https: require('https') | ||
}; | ||
var url = require('url'); | ||
var debug = require('debug')('simple-http-proxy'); | ||
var protocols = { | ||
http: require('http'), | ||
https: require('https') | ||
}; | ||
@@ -24,3 +24,3 @@ /** | ||
module.exports = function(endpoint, opts) { | ||
if(!opts) opts = {}; | ||
if (!opts) opts = {}; | ||
@@ -53,3 +53,3 @@ var parsedUrl = url.parse(endpoint); | ||
// Optionally delete cookie | ||
if(opts.cookies === false) delete req.headers.cookie; | ||
if (opts.cookies === false) delete req.headers.cookie; | ||
@@ -69,3 +69,3 @@ // Resolve the url | ||
// Enable forwarding headers | ||
if(xforward) { | ||
if (xforward) { | ||
// Get the path at which the middleware is mounted | ||
@@ -77,3 +77,3 @@ var resPath = req.originalUrl | ||
// We'll need to add a / if it's not on there | ||
if(resPath.indexOf('/') !== 0) resPath = '/' + resPath; | ||
if (resPath.indexOf('/') !== 0) resPath = '/' + resPath; | ||
@@ -96,10 +96,14 @@ // Pass along our headers | ||
* transfer-encoding: chunked | ||
* | ||
* | ||
* 0 | ||
* | ||
*/ | ||
if(~['POST', 'DELETE'].indexOf(req.method) && options.headers['transfer-encoding'] != 'chunked') { | ||
if (~['POST', 'DELETE'].indexOf(req.method) && options.headers['transfer-encoding'] != 'chunked') { | ||
options.headers['content-length'] = options.headers['content-length'] || '0'; | ||
} | ||
// allow the caller to change the options | ||
if (opts.onrequest) opts.onrequest(options, req); | ||
debug('sending proxy request', options); | ||
@@ -120,2 +124,5 @@ | ||
// allow the caller to override the default pipe behavior | ||
if (opts.onresponse && opts.onresponse(response, res)) return; | ||
res.writeHead(response.statusCode, response.headers); | ||
@@ -129,3 +136,3 @@ | ||
// Handle any timeouts that occur | ||
request.setTimeout(opts.timeout || 10000, function() { | ||
if (opts.timeout !== false) request.setTimeout(opts.timeout || 10000, function() { | ||
// Clean up the socket | ||
@@ -137,3 +144,3 @@ request.setSocketKeepAlive(false); | ||
res.status(504); | ||
next(new Error('Proxy to "'+endpoint+'" timed out')); | ||
next(new Error('Proxy to "' + endpoint + '" timed out')); | ||
}); | ||
@@ -140,0 +147,0 @@ |
{ | ||
"name": "simple-http-proxy", | ||
"version": "0.5.10", | ||
"version": "0.5.11", | ||
"description": "Simple proxy middleware", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,4 +14,4 @@ simple-http-proxy [](https://travis-ci.org/flokk/simple-http-proxy) | ||
*/ | ||
var express = require("express") | ||
, proxy = require("simple-http-proxy"); | ||
var express = require('express'); | ||
var proxy = require('simple-http-proxy'); | ||
@@ -26,3 +26,3 @@ /** | ||
*/ | ||
app.use("/api", proxy("http://my.other.host.com/path-to-proxy")); | ||
app.use('/api', proxy('http://my.other.host.com/path-to-proxy')); | ||
``` | ||
@@ -40,17 +40,49 @@ | ||
``` | ||
// snip | ||
app.use('/api', proxy('http://my.other.host.com/path-to-proxy', opts)); | ||
``` | ||
app.use("/api", proxy("http://my.other.host.com/path-to-proxy", { | ||
### Options | ||
// Disable sending cookies; on by deafult | ||
cookies: false, | ||
#### `cookies` | ||
// Add x-forwarded-* headers to proxy request | ||
xforward: true, | ||
Disable sending cookies by passing `false`; on by deafult. | ||
// Change the timeout length of the proxy (defaults to 10 seconds) | ||
timeout: 20000 | ||
})); | ||
#### `xforward` | ||
// snip | ||
Setting this to `true` will set `x-forwarded-proto`, `x-forwarded-host`, `x-forwarded-port` and `x-forwarded-path` headers. | ||
Passing an object will override the header names: | ||
```js | ||
{ | ||
proto: 'x-orig-proto', | ||
host: 'x-orig-host', | ||
port: 'x-orig-port', | ||
path: 'x-orig-path' | ||
} | ||
``` | ||
#### `timeout` | ||
A positive millisecond value for the timeout of the request. Defaults to 10000 (10s). | ||
Setting it to `false` will disable the timeout. | ||
#### `onrequest` | ||
A function to be called on each request. The first parameter will be the [options object](http://nodejs.org/api/http.html#http_http_request_options_callback) for the http request. The second will be the [request object](http://nodejs.org/api/http.html#http_class_http_clientrequest). | ||
This can be used to change any of the http options for a given request. | ||
#### `onresponse` | ||
A function to be called on each response. The first parameter will be the [incoming message](http://nodejs.org/api/http.html#http_http_incomingmessage) for a given request. The second will be the [server response object](http://nodejs.org/api/http.html#http_class_http_serverresponse). | ||
If this function returns `true` the default pipe will not be used and will be up to the `onresponse` function to implement that behavior. This is useful for rewriting responses that are sent to the client. | ||
Tests | ||
----- | ||
```sh | ||
$ npm test | ||
``` |
9972
17.58%208
1.96%86
59.26%