Skip to content

Proxy config secure flag #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
};
Expand Down
17 changes: 11 additions & 6 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/proxy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
});

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
},
{
"name": "Johan Vromans"
},
{
"name": "tomtucker18"
}
],
"engines": {
Expand Down