forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
46 lines (39 loc) · 1.49 KB
/
proxy.ts
File metadata and controls
46 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as express from 'express';
import * as http from 'http';
import {writeFile} from '../../utils/fs';
import {request} from '../../utils/http';
import {killAllProcesses, ng} from '../../utils/process';
import {ngServe} from '../../utils/project';
import {expectToFail} from '../../utils/utils';
export default function() {
// Create an express app that serves as a proxy.
const app = express();
const server = http.createServer(app);
server.listen(0);
app.set('port', server.address().port);
app.get('/api/test', function (req, res) {
res.send('TEST_API_RETURN');
});
const backendHost = 'localhost';
const backendPort = server.address().port;
const proxyServerUrl = `http://${backendHost}:${backendPort}`;
const proxyConfigFile = 'proxy.config.json';
const proxyConfig = {
'/api/*': {
target: proxyServerUrl
}
};
return Promise.resolve()
.then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2)))
.then(() => ngServe('--proxy', proxyConfigFile))
.then(() => request('http://localhost:4200/api/test'))
.then(body => {
if (!body.match(/TEST_API_RETURN/)) {
throw new Error('Response does not match expected value.');
}
})
.then(() => server.close(), (err) => { server.close(); throw err; })
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
// A non-existing proxy file should error.
.then(() => expectToFail(() => ng('serve', '--proxy', 'proxy.non-existent.json')));
}