From 403d03fde456940d827a3efc7f0f923acb369598 Mon Sep 17 00:00:00 2001 From: tomtucker18 <48351489+tomtucker18@users.noreply.github.com> Date: Thu, 12 Jan 2023 09:42:11 +0100 Subject: [PATCH 1/6] add secure config option to proxy --- lib/proxy.js | 2 +- package.json | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/proxy.js b/lib/proxy.js index aad5daf..d1af5c8 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -41,7 +41,7 @@ function proxyHandler(req, res, configs) { ); if (proxyServer === null) { - proxyServer = httpProxy.createProxyServer({}); + proxyServer = httpProxy.createProxyServer({secure: config.secure === undefined ? true : config.secure}); } proxyServer.web(req, res, { diff --git a/package.json b/package.json index 7717bff..cadcbd5 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,9 @@ }, { "name": "AVierwind" + }, + { + "name": "tomtucker18" } ], "engines": { From f6a50ea27c09d44655c04523b4772c66654d72cb Mon Sep 17 00:00:00 2001 From: tomtucker18 <48351489+tomtucker18@users.noreply.github.com> Date: Thu, 12 Jan 2023 09:59:46 +0100 Subject: [PATCH 2/6] update package-lock.json --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9f27e67..f757731 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "angular-http-server", - "version": "1.10.1", + "version": "1.11.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "angular-http-server", - "version": "1.10.1", + "version": "1.11.1", "license": "ISC", "dependencies": { "http-proxy": "^1.18.1", @@ -24,7 +24,7 @@ "node-mocks-http": "^1.11.0" }, "engines": { - "node": ">=8.0.0" + "node": ">=10.0.0" } }, "node_modules/accepts": { From 7d3823def329a6298cf2607cec83475df6230f3a Mon Sep 17 00:00:00 2001 From: tomtucker18 <48351489+tomtucker18@users.noreply.github.com> Date: Thu, 12 Jan 2023 10:29:00 +0100 Subject: [PATCH 3/6] fix failing proxy test --- lib/proxy.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/proxy.spec.js b/lib/proxy.spec.js index f9c114a..30fadba 100644 --- a/lib/proxy.spec.js +++ b/lib/proxy.spec.js @@ -44,7 +44,7 @@ describe("proxyHandler", () => { }); expect(() => proxyHandler(req, {}, null, false)).to.throw( - "no proxyConfig present, please configure in your config file" + "no proxy configs present, please configure in your config file" ); }); From 1f4055c32f2e18e0e833e80443688de9de5233da Mon Sep 17 00:00:00 2001 From: tomtucker18 <48351489+tomtucker18@users.noreply.github.com> Date: Thu, 12 Jan 2023 11:28:24 +0100 Subject: [PATCH 4/6] update readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c0b377f..b64975b 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ angular-http-server --config configs/angular-http-server.config.js --useProxy tr To configure the proxy add a proxy object to your config file. The proxy should be an array of configs with two required properties: a forward property which must be a string array listing url parts which should trigger the proxy, and a target property which should define the target to proxy to. -The config can also contain an optional protocol option, when this is absent the server will default to https +The config can also contain an optional protocol option, when this is absent the server will default to https. With the optional secure option you can define if the proxy should verify SSL certificates. When no secure option is specified, it will use true as default. simple example: @@ -142,6 +142,7 @@ module.exports = { { forward: ["api/example-api-2", "api-proxy-2/example"], target: "localhost:6000", + secure: false }, ], }; From 8d9680462489af5c732aa8e264e929774b8e4d0a Mon Sep 17 00:00:00 2001 From: tomtucker18 <48351489+tomtucker18@users.noreply.github.com> Date: Thu, 21 Dec 2023 14:34:16 +0100 Subject: [PATCH 5/6] feat: add full proxy config support --- lib/proxy.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/proxy.js b/lib/proxy.js index d1af5c8..97824a8 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -35,22 +35,27 @@ function proxyHandler(req, res, configs) { (url) => req.url.indexOf(url) !== -1 ); if (proxyHit) { + // Clone the proxy config in order to make changes without affecting the original config object + const proxyConfig = { ...config }; + const protocol = config.protocol ? config.protocol : "https"; + proxyConfig.target = `${protocol}://${proxyConfig.target}`; console.log( - `Proxying request ${req.method}, ${req.url} to ${protocol}://${config.target}` + `Proxying request ${req.method}, ${req.url} to ${proxyConfig.target}` ); + // Remove "forward" property since we've already handled the forwarding logic + delete proxyConfig["forward"]; + if (proxyServer === null) { - proxyServer = httpProxy.createProxyServer({secure: config.secure === undefined ? true : config.secure}); + proxyServer = httpProxy.createProxyServer(proxyConfig); } - proxyServer.web(req, res, { - target: `${protocol}://${config.target}`, - }, (e, req, res) => { + proxyServer.web(req, res, (e, req, res) => { console.error(`Proxy error: ${e.code}`); res.writeHead(502); res.end(); - }); + }); } } }); From 68ccac9c82952e473fd1e53bc6eeed152598c1d9 Mon Sep 17 00:00:00 2001 From: tomtucker18 <48351489+tomtucker18@users.noreply.github.com> Date: Thu, 21 Dec 2023 14:53:53 +0100 Subject: [PATCH 6/6] docs: update README.md --- README.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 60069a2..73591f4 100644 --- a/README.md +++ b/README.md @@ -119,36 +119,38 @@ module.exports = (argv) => { ## Http proxy -#### Enabling proxy - The server contains a simple http proxy. The proxy must be configured in the config file. -To enable this proxy: - -```sh -angular-http-server --config configs/angular-http-server.config.js --useProxy true -``` -#### configuring proxy +### Configuring proxy +#### Basic proxy config To configure the proxy add a proxy object to your config file. -The proxy should be an array of configs with two required properties: a forward property which must be a string array listing url parts which should trigger the proxy, and a target property which should define the target to proxy to. -The config can also contain an optional protocol option, when this is absent the server will default to https. With the optional secure option you can define if the proxy should verify SSL certificates. When no secure option is specified, it will use true as default. +The proxy should be an array of configs with two required properties: a `forward` property which must be a string array listing url parts which should trigger the proxy, and a `target` property which should define the target to proxy to. +The config can also contain an optional `protocol` option, when this is absent the server will default to https. + +#### Additional proxy options + +For more advanced configurations you can add properties from the [node-http-proxy](https://github.com/http-party/node-http-proxy#options) options. For example a `secure: false` option to disable ssl verification. -simple example: +#### Example ```js module.exports = { proxy: [ { + // Basic config forward: ["api/example-api", "api-proxy/example"], target: "localhost:5000", protocol: "http", }, { + // Advanced config forward: ["api/example-api-2", "api-proxy-2/example"], target: "localhost:6000", - secure: false + secure: false, + auth: "admin:secretPW", + proxyTimeout: 8000, }, ], };