Skip to content

Commit 7466b91

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

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

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

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
},

packages/@angular/cli/lib/config/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,11 @@
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",
480+
"default": ""
476481
}
477482
}
478483
}

tests/e2e/tests/misc/proxy-config.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
36+
const app = configJson.defaults;
37+
app.serve = {
38+
proxyConfig: proxyConfigFile
39+
};
40+
}))
41+
.then(() => ngServe())
42+
.then(() => request('http://localhost:4200/api/test'))
43+
.then(body => {
44+
if (!body.match(/TEST_API_RETURN/)) {
45+
throw new Error('Response does not match expected value.');
46+
}
47+
})
48+
.then(() => server.close(), (err) => { server.close(); throw err; })
49+
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
50+
51+
// A non-existing proxy file should error.
52+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
53+
const app = configJson.defaults;
54+
app.serve = {
55+
proxyConfig: 'proxy.non-existent.json'
56+
};
57+
}))
58+
.then(() => expectToFail(() => ngServe()));
59+
}

0 commit comments

Comments
 (0)