diff --git a/README.md b/README.md index b925f35..6b99784 100644 --- a/README.md +++ b/README.md @@ -121,35 +121,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 +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, + auth: "admin:secretPW", + proxyTimeout: 8000, }, ], }; diff --git a/lib/proxy.js b/lib/proxy.js index aad5daf..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({}); + 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(); - }); + }); } } }); 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" ); }); diff --git a/package.json b/package.json index 511d42c..16c5404 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,9 @@ }, { "name": "Johan Vromans" + }, + { + "name": "tomtucker18" } ], "engines": {