Skip to content

Commit 68fe938

Browse files
authored
feat: Live check for chart stock market app (#516)
1 parent 6713127 commit 68fe938

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Diff for: apps/chart-the-stock-market/index.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ require('dotenv').config();
77
const app = express();
88
app.use(express.static('public'));
99

10+
app.get('/status/ping', (req, res) => {
11+
res.status(200).send({ msg: 'pong' });
12+
});
13+
1014
const portNum = process.env.PORT || 3000;
1115

1216
const server = app.listen(portNum, () => {
@@ -20,13 +24,16 @@ function emitStockData() {
2024
io.emit('updateStockData', JSON.stringify(currentStockData));
2125
}
2226

23-
io.on('connection', function(socket) {
27+
io.on('connection', function (socket) {
2428
emitStockData();
2529

26-
socket.on('newStock', function(stock) {
27-
if(!(stock.symbol in currentStockData) && Object.keys(currentStockData).length < 4) {
30+
socket.on('newStock', function (stock) {
31+
if (
32+
!(stock.symbol in currentStockData) &&
33+
Object.keys(currentStockData).length < 4
34+
) {
2835
const requestUrl = `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${stock.symbol}&apikey=${process.env.APIKEY}`;
29-
36+
3037
https.get(requestUrl, res => {
3138
let stockData = '';
3239
res.setEncoding('utf8');
@@ -38,14 +45,16 @@ io.on('connection', function(socket) {
3845
res.on('end', () => {
3946
stockData = JSON.parse(stockData);
4047

41-
if (stockData["Error Message"]) { //stock symbol requested doesn't exist
48+
if (stockData['Error Message']) {
49+
//stock symbol requested doesn't exist
4250
io.emit('stopLoading');
43-
} else { //stock symbol requested found
44-
currentStockData[stock.symbol] = stockData['Time Series (Daily)']
51+
} else {
52+
//stock symbol requested found
53+
currentStockData[stock.symbol] = stockData['Time Series (Daily)'];
4554
emitStockData();
4655
}
4756

48-
res.on('error', (e) => {
57+
res.on('error', e => {
4958
console.log('error fetching stock data');
5059
console.error(e);
5160
io.emit('stopLoading');
@@ -57,10 +66,10 @@ io.on('connection', function(socket) {
5766
}
5867
});
5968

60-
socket.on('deleteStock', function(stock) {
69+
socket.on('deleteStock', function (stock) {
6170
console.log('delete' + stock);
6271
console.log(stock.symbol);
63-
if(stock.symbol in currentStockData) {
72+
if (stock.symbol in currentStockData) {
6473
delete currentStockData[stock.symbol];
6574

6675
emitStockData();

0 commit comments

Comments
 (0)