-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
124 lines (102 loc) · 3.55 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require('newrelic');
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
const express = require('express');
const path = require('path');
const axios = require('axios');
const app = express();
const port = process.env.PORT || 3000;
app.use('/', express.static(path.join(__dirname, 'public')));
app.use('/stocks/:ticker', express.static(path.join(__dirname, 'public')));
const axios3001 = axios.create({
baseURL: 'http://localhost:3001',
});
const axios3002 = axios.create({
baseURL: 'http://localhost:3002',
});
const axios3003 = axios.create({
baseURL: 'http://localhost:3003',
});
const axios4000 = axios.create({
baseURL: 'http://localhost:4000',
});
app.use('/api/ratings/:ticker', (req, res) => {
axios3001.get(`/api/ratings/${req.params.ticker}`)
.then(response => res.send(response.data))
.catch(err => res.send(err));
})
app.use('/api/history/:ticker', (req, res) => {
axios3001.get(`/api/history/${req.params.ticker}`)
.then(response => res.send(response.data))
.catch(err => res.send(err));
})
app.use('/api/stocks/:ticker', (req, res) => {
axios3002.get(`/api/stocks/${req.params.ticker}`)
.then(response => res.send(response.data))
.catch(err => res.send(err));
})
app.use('/api/accounts/:account_number', (req, res) => {
axios3002.get(`/api/accounts/${req.params.account_number}`)
.then(response => res.send(response.data))
.catch(err => res.send(err));
})
app.get('/api/about/:symbol', (req, res) => {
axios3003.get(`/api/about/${req.params.symbol}`)
.then(response => res.send(response.data))
.catch(err => res.send(err));
})
app.delete('/api/about/:symbol', (req, res) => {
axios3003.delete(`/api/about/${req.params.symbol}`)
.then(count => res.send(count))
.catch(err => res.send(err));
})
app.post('/api/about/', (req, res) => {
axios3003.post(`/api/about/`)
.then(response => res.send(response.data))
.catch(err => res.send(err));
})
app.get('/api/users/:userId', (req, res) => {
axios3003.get(`/api/users/${req.params.userId}`)
.then(response => res.send(response.data))
.catch(err => res.send(err));
})
app.delete('/api/users/:userId', (req, res) => {
axios3003.delete(`/api/users/${req.params.userId}`)
.then(response => res.send(response.data))
.catch(err => res.send(err));
})
app.post('/api/users', (req, res) => {
axios3003.get(`/api/users`)
.then(response => res.send(response.data))
.catch(err => res.send(err));
})
// app.use('/stocks/tags/:tag', (req, res) => {
// axios3003.get(`/stocks/tags/${req.params.tag}`)
// .then(response => res.send(response.data))
// .catch(err => res.send(err));
// })
// app.use('/api/:stockId', (req, res) => {
// axios4000.get(`/api/${req.params.stockId}`)
// .then(response => res.send(response.data))
// .catch(err => res.send(err));
// })
// app.get('/:stockId', (req, res) => {
// axios4000.get(`/api/${req.params.stockId}`)
// .then(response => res.send(response.data))
// .catch(err => res.send(err));
// })
app.listen(port, () => {
console.log(`proxy server running at: http://localhost:${port}`);
});
}