-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathserver.js
84 lines (78 loc) · 2.73 KB
/
server.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var mockserver = require('mockserver-node');
var mockServerClient = require('mockserver-client').mockServerClient;
var HTTP_PORT = 1080;
mockserver
.start_mockserver({
serverPort: HTTP_PORT
})
.then(function () {
// forward backend REST API request to local machine
mockServerClient("localhost", HTTP_PORT)
.mockAnyResponse({
"httpRequest": {
"path": "/rest.*"
},
"httpForward": { // local machine Tomcat instance
"host": "127.0.0.1",
"port": 8080,
"scheme": "HTTP"
},
"times": {
"unlimited": true
}
})
.then(
function () {
// forward all other request to QA environment
mockServerClient("localhost", HTTP_PORT)
.mockAnyResponse({
"httpRequest": {
"path": "/.*"
},
"httpForward": { // QA environment load balancer
"host": "192.168.50.10",
"port": 443,
"scheme": "HTTPS"
},
"times": {
"unlimited": true
}
})
.then(
function () {
console.log("created expectations");
},
function (error) {
console.log(error);
}
);
},
function (error) {
console.log(error);
}
);
});
console.log("started on port: " + HTTP_PORT);
// stop MockServer if Node exist abnormally
process.on('uncaughtException', function (err) {
console.log('uncaught exception - stopping node server: ' + JSON.stringify(err));
mockserver.stop_mockserver();
throw err;
});
// stop MockServer if Node exits normally
process.on('exit', function () {
console.log('exit - stopping node server');
mockserver.stop_mockserver();
});
// stop MockServer when Ctrl-C is used
process.on('SIGINT', function () {
console.log('SIGINT - stopping node server');
mockserver.stop_mockserver();
process.exit(0);
});
// stop MockServer when a kill shell command is used
process.on('SIGTERM', function () {
console.log('SIGTERM - stopping node server');
mockserver.stop_mockserver();
process.exit(0);
});