Skip to content

Commit

Permalink
Add support for multiple proxy servers and proxy path rewrites.
Browse files Browse the repository at this point in the history
  • Loading branch information
galbi101 committed Jul 11, 2017
1 parent e7e79ff commit 205815c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm install -g server-with-benefits
Then, to setup your server(s), edit **_swbConfig.json** that comes with the package and rename it to **swbConfig.json**.
You can place this file anywhere you like, but make sure you then start the server (see [Usage](#usage)) from the directory where it's placed.
Alternatively (and *preferably*), you can avoid this restriction by setting an environment variable named **SWB_CONF_FILE**
with the file's full path as its value (e.g. SWB_CONF_FILE=C:\dev\swbConfig.json).
with the file's full path as its value (e.g. SWB_CONF_FILE=C:\dev\swbConfig.json), or by passing the path as an only argument.

The configuration file should be of the following format:

Expand Down Expand Up @@ -72,5 +72,5 @@ The configuration file should be of the following format:
## Usage

```sh
$ swb
$ swb [config-file-path]
```
16 changes: 9 additions & 7 deletions _swbConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"/lib": "/lib" // i.e. http://<your_host_name>/lib -> <srcDir>/lib
}
},
"proxy": { // (OPTIONAL) A proxy to a remote server for some path patterns
"target": { // can be an object with host and port, or a full url string e.g. "http://myproxy:80"
"host": "hostname",
"port": 80
},
"pathPatterns": [] // e.g. ["^/api/"]
},
"proxy": [ // (OPTIONAL) proxy servers for some path patterns
{
"target": { // can be an object with host and port, or a full url string e.g. "http://myproxy:80"
"host": "hostname",
"port": 80
},
"pathPatterns": [] // e.g. ["^/api/"] or [{"^/api/": "/"}]
}
],
"delay": { // (OPTIONAL) Adding delay to responses for some path patterns
"pathPatterns": [], // e.g. ["^/api/"],
"time": 2000 // the delay amount
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "server-with-benefits",
"author": "Gal Ben-Ishay <[email protected]>",
"version": "0.12.4",
"version": "0.13.0",
"engines": {
"node": ">=0.10"
},
Expand Down
33 changes: 25 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ var getFixtureCheckRequestHandler = function(fixtures) {
}
};
};
var getProxyCheckRequestHandler = function(pathRegExp, proxyTarget) {
var getProxyCheckRequestHandler = function(pathRegExp, proxyTarget, pathRewrite) {
return function(req, res, next) {
if (pathRegExp.test(req.url)) {
if (pathRewrite != null) {
req.url = req.url.replace(new RegExp(pathRegExp), pathRewrite);
}
proxy.web(req, res, {
target: proxyTarget
});
Expand All @@ -87,11 +90,9 @@ var getProxyCheckRequestHandler = function(pathRegExp, proxyTarget) {

conf.servers.forEach(function(serverConf) {
var app = express(),
pathRegExp,
messages = "\n" + headerStyle("Serving ") + (serverConf.static && serverConf.static.srcDir ? fileStyle(serverConf.static.srcDir) + " " : "") + "on port " + portStyle(serverConf.port);
if (serverConf.delay) {
pathRegExp = new RegExp("(?:" + serverConf.delay.pathPatterns.join(")|(?:") + ")");
app.use(getDelayCheckRequestHandler(pathRegExp, serverConf.delay.time));
app.use(getDelayCheckRequestHandler(new RegExp("(?:" + serverConf.delay.pathPatterns.join(")|(?:") + ")"), serverConf.delay.time));
messages += "\n" + featureStyle("Delay") + " of " + delayTimeStyle(serverConf.delay.time + " ms") + " is activated for path patterns: " + boldStyle(serverConf.delay.pathPatterns.join(", "));
}
if (serverConf.fixtures) {
Expand All @@ -114,10 +115,26 @@ conf.servers.forEach(function(serverConf) {
app.use(getFixtureCheckRequestHandler(activeFixtures));
}
if (serverConf.proxy) {
pathRegExp = new RegExp("(?:" + serverConf.proxy.pathPatterns.join(")|(?:") + ")");
app.use(getProxyCheckRequestHandler(pathRegExp, serverConf.proxy.target));
messages += "\n" + featureStyle("Redirecting") + " path patterns: " + boldStyle(serverConf.proxy.pathPatterns.join(", ")) + " to "
+ proxyStyle(typeof serverConf.proxy.target == 'string' ? serverConf.proxy.target : (serverConf.proxy.target.host + ":" + serverConf.proxy.target.port));
serverConf.proxy.forEach(function(proxy) {
var simplePaths = [];
var pathRewrites = [];
proxy.pathPatterns.forEach(function(path) {
(typeof path == 'string' ? simplePaths : pathRewrites).push(path);
});
if (simplePaths.length) {
app.use(getProxyCheckRequestHandler(new RegExp("(?:" + simplePaths.join(")|(?:") + ")"), proxy.target));
messages += "\n" + featureStyle("Redirecting") + " path patterns: " + boldStyle(simplePaths.join(", ")) + " to "
+ proxyStyle((typeof proxy.target == 'string' ? proxy.target : (proxy.target.host + ":" + proxy.target.port)) + " (same path)");
}
pathRewrites.forEach(function(pathObj) {
var pathRegExp = Object.keys(pathObj)[0];
var pathRewrite = pathObj[pathRegExp];
app.use(getProxyCheckRequestHandler(new RegExp(pathRegExp), proxy.target, pathRewrite));
messages += "\n" + featureStyle("Redirecting") + " path patterns: " + boldStyle(pathRegExp) + " to "
+ proxyStyle((typeof proxy.target == 'string' ? proxy.target : (proxy.target.host + ":" + proxy.target.port)) + "/" + pathRewrite);
});
});

}
if (serverConf.static && serverConf.static.srcDir && serverConf.static.paths) {
for (var path in serverConf.paths) {
Expand Down

0 comments on commit 205815c

Please sign in to comment.