diff --git a/.gitignore b/.gitignore index 3c3629e..eb79dd5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.idea diff --git a/README.md b/README.md index 4a50f6e..fddfa04 100644 --- a/README.md +++ b/README.md @@ -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: @@ -72,5 +72,5 @@ The configuration file should be of the following format: ## Usage ```sh -$ swb +$ swb [config-file-path] ``` diff --git a/_swbConfig.json b/_swbConfig.json index 6e57cf2..fab2435 100644 --- a/_swbConfig.json +++ b/_swbConfig.json @@ -9,13 +9,15 @@ "/lib": "/lib" // i.e. http:///lib -> /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 diff --git a/package.json b/package.json index 0f1b906..d55558e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "server-with-benefits", "author": "Gal Ben-Ishay ", - "version": "0.12.4", + "version": "0.13.0", "engines": { "node": ">=0.10" }, diff --git a/server.js b/server.js index 5100525..5dff6f6 100644 --- a/server.js +++ b/server.js @@ -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 }); @@ -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) { @@ -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) {