Skip to content

Commit fe43113

Browse files
committed
feat(@angular/cli): read proxyConfig from angular-cli.json
easy proxy config by reading default from angular-cli.json (#6240)
1 parent 0d3d9ef commit fe43113

File tree

5 files changed

+79
-46
lines changed

5 files changed

+79
-46
lines changed

Diff for: docs/documentation/angular-cli.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
- *ssl* (`boolean`): Enables ssl for the application. Default is `false`.
8383
- *sslKey* (`string`): The ssl key used by the server. Default is `ssl/server.key`.
8484
- *sslCert* (`string`): The ssl certificate used by the server. Default is `ssl/server.crt`.
85+
- *proxyConfig* (`string`): Proxy configuration file.
8586

8687
- **packageManager** (`string`): Specify which package manager tool to use. Options include `npm`, `cnpm` and `yarn`.
8788

Diff for: packages/@angular/cli/commands/serve.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const defaultHost = config.get('defaults.serve.host');
1414
const defaultSsl = config.get('defaults.serve.ssl');
1515
const defaultSslKey = config.get('defaults.serve.sslKey');
1616
const defaultSslCert = config.get('defaults.serve.sslCert');
17+
const defaultProxyConfig = config.get('defaults.serve.proxyConfig');
1718

1819
export interface ServeTaskOptions extends BuildOptions {
1920
port?: number;
@@ -49,6 +50,7 @@ export const baseServeCommandOptions: any = overrideOptions([
4950
{
5051
name: 'proxy-config',
5152
type: 'Path',
53+
default: defaultProxyConfig,
5254
aliases: ['pc'],
5355
description: 'Proxy configuration file.'
5456
},

Diff for: packages/@angular/cli/lib/config/schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@
473473
"description": "The ssl certificate used by the server.",
474474
"type": "string",
475475
"default": "ssl/server.crt"
476+
},
477+
"proxyConfig": {
478+
"description": "Proxy configuration file.",
479+
"type": "string"
476480
}
477481
}
478482
}

Diff for: tests/e2e/tests/misc/proxy-config.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as express from 'express';
2+
import * as http from 'http';
3+
4+
import {writeFile} from '../../utils/fs';
5+
import { request } from '../../utils/http';
6+
import { assetDir } from '../../utils/assets';
7+
import { killAllProcesses } from '../../utils/process';
8+
import { ngServe } from '../../utils/project';
9+
import { updateJsonFile } from '../../utils/project';
10+
import {expectToFail} from "../../utils/utils";
11+
12+
export default function() {
13+
// Create an express app that serves as a proxy.
14+
const app = express();
15+
const server = http.createServer(app);
16+
server.listen(0);
17+
18+
app.set('port', server.address().port);
19+
app.get('/api/test', function (req, res) {
20+
res.send('TEST_API_RETURN');
21+
});
22+
23+
const backendHost = 'localhost';
24+
const backendPort = server.address().port;
25+
const proxyServerUrl = `http://${backendHost}:${backendPort}`;
26+
const proxyConfigFile = 'proxy.config.json';
27+
const proxyConfig = {
28+
'/api/*': {
29+
target: proxyServerUrl
30+
}
31+
};
32+
33+
return Promise.resolve()
34+
.then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2)))
35+
36+
.then(() => ngServe('--proxy-config', proxyConfigFile))
37+
.then(() => request('http://localhost:4200/api/test'))
38+
.then(body => {
39+
if (!body.match(/TEST_API_RETURN/)) {
40+
throw new Error('Response does not match expected value.');
41+
}
42+
})
43+
.then(() => server.close(), (err) => { server.close(); throw err; })
44+
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
45+
46+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
47+
const app = configJson.defaults;
48+
app.serve = {
49+
proxyConfig: proxyConfigFile
50+
};
51+
}))
52+
.then(() => ngServe())
53+
.then(() => request('http://localhost:4200/api/test'))
54+
.then(body => {
55+
if (!body.match(/TEST_API_RETURN/)) {
56+
throw new Error('Response does not match expected value.');
57+
}
58+
})
59+
.then(() => server.close(), (err) => { server.close(); throw err; })
60+
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
61+
62+
// A non-existing proxy file should error.
63+
64+
.then(() => expectToFail(() => ng('serve', '--proxy-config', 'proxy.non-existent.json')))
65+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
66+
const app = configJson.defaults;
67+
app.serve = {
68+
proxyConfig: 'proxy.non-existent.json'
69+
};
70+
}))
71+
.then(() => expectToFail(() => ngServe()));
72+
}

Diff for: tests/e2e/tests/misc/proxy.ts

-46
This file was deleted.

0 commit comments

Comments
 (0)