-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
100 lines (83 loc) · 2.6 KB
/
app.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const http = require('http');
const express = require('express');
const proxy = require('http-proxy-middleware');
const config = require('./config');
let childProcess = require('child_process'),
backend,
client;
client = childProcess.exec('node modules/client/dist/server', {
env: {
PORT: process.env.CLIENT_PORT ? process.env.CLIENT_PORT : 8080
}
}, function (error, stdout, stderr) {
if (error) {
console.log(error.stack);
console.log('Client error code: ' + error.code);
console.log('Client signal received: ' + error.signal);
return;
}
console.log(`Client stdout: ${stdout}`);
console.log(`Client stderr: ${stderr}`);
});
client.on('exit', function (code) {
console.log('Client process exited with exit code ' + code);
process.exit(code);
});
backend = childProcess.exec('node modules/backend/src', function (error, stdout, stderr) {
if (error) {
console.log(error.stack);
console.log('Backend error code: ' + error.code);
console.log('Backend signal received: ' + error.signal);
return;
}
console.log(`Backend stdout: ${stdout}`);
console.log(`Backend stderr: ${stderr}`);
});
backend.on('exit', function (code) {
console.log('Backend process exited with exit code ' + code);
process.exit(code);
});
process.on('exit', function (code) {
console.log('Main process exited with exit code ' + code);
try {
backend.kill();
} catch (e) {
console.log('Error!', e);
}
try {
client.kill();
} catch (e) {
console.log('Error!', e);
}
});
client.stdout.on('data', function(data) {
console.log(`Client stdout: ${data}`);
});
client.stderr.on('data', function(data) {
console.error(`Client stderr: ${data}`);
});
backend.stdout.on('data', function(data) {
console.log(`Backend stdout: ${data}`);
});
backend.stderr.on('data', function(data) {
console.error(`Backend stderr: ${data}`);
});
// proxy middleware options
const serverOptions = {
target: 'http://localhost:8080', // target host
changeOrigin: true, // needed for virtual hosted sites
ws: true // proxy websockets
};
// proxy middleware options
const clientOptions = {
target: 'http://localhost:3030', // target host
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
pathRewrite: {
'^/api' : '/', // rewrite path
},
};
const app = express();
app.use('/api', proxy(clientOptions));
app.use('/', proxy(serverOptions));
app.listen(80);