@@ -7,6 +7,10 @@ require('dotenv').config();
7
7
const app = express ( ) ;
8
8
app . use ( express . static ( 'public' ) ) ;
9
9
10
+ app . get ( '/status/ping' , ( req , res ) => {
11
+ res . status ( 200 ) . send ( { msg : 'pong' } ) ;
12
+ } ) ;
13
+
10
14
const portNum = process . env . PORT || 3000 ;
11
15
12
16
const server = app . listen ( portNum , ( ) => {
@@ -20,13 +24,16 @@ function emitStockData() {
20
24
io . emit ( 'updateStockData' , JSON . stringify ( currentStockData ) ) ;
21
25
}
22
26
23
- io . on ( 'connection' , function ( socket ) {
27
+ io . on ( 'connection' , function ( socket ) {
24
28
emitStockData ( ) ;
25
29
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
+ ) {
28
35
const requestUrl = `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${ stock . symbol } &apikey=${ process . env . APIKEY } ` ;
29
-
36
+
30
37
https . get ( requestUrl , res => {
31
38
let stockData = '' ;
32
39
res . setEncoding ( 'utf8' ) ;
@@ -38,14 +45,16 @@ io.on('connection', function(socket) {
38
45
res . on ( 'end' , ( ) => {
39
46
stockData = JSON . parse ( stockData ) ;
40
47
41
- if ( stockData [ "Error Message" ] ) { //stock symbol requested doesn't exist
48
+ if ( stockData [ 'Error Message' ] ) {
49
+ //stock symbol requested doesn't exist
42
50
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)' ] ;
45
54
emitStockData ( ) ;
46
55
}
47
56
48
- res . on ( 'error' , ( e ) => {
57
+ res . on ( 'error' , e => {
49
58
console . log ( 'error fetching stock data' ) ;
50
59
console . error ( e ) ;
51
60
io . emit ( 'stopLoading' ) ;
@@ -57,10 +66,10 @@ io.on('connection', function(socket) {
57
66
}
58
67
} ) ;
59
68
60
- socket . on ( 'deleteStock' , function ( stock ) {
69
+ socket . on ( 'deleteStock' , function ( stock ) {
61
70
console . log ( 'delete' + stock ) ;
62
71
console . log ( stock . symbol ) ;
63
- if ( stock . symbol in currentStockData ) {
72
+ if ( stock . symbol in currentStockData ) {
64
73
delete currentStockData [ stock . symbol ] ;
65
74
66
75
emitStockData ( ) ;
0 commit comments