-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
110 lines (91 loc) · 2.57 KB
/
Copy pathserver.js
File metadata and controls
110 lines (91 loc) · 2.57 KB
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
var express = require('express');
var app = express();
app.use(express.static('public'));
app.use(express.json());
app.listen(8080, () => {
console.log("server running at http://localhost:8080");
});
const stocks = require('./scripts/stocks.js');
const search = require('./scripts/search.js');
const user = require('./scripts/user.js');
const news = require('./scripts/news.js');
const https = require('https');
const fs = require('fs');
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 3000 })
wss.on('connection', ws => {
console.log("websocket opened");
ws.on('message', message => {
let data = JSON.parse(message.toString());
console.log("received message from client");
stocks.openWebSocket(data["symbol"],
function(data) {
console.log("received data from finnhub");
ws.send(JSON.stringify(data));
},
function(e) {
console.log("received error: "+e.data);
ws.send(JSON.stringify({
"type": "error",
"message": e.data
}));
},
function(e) {
console.log("opened connection to finnhub");
ws.send(JSON.stringify({
"type": "message",
"message": "connection to finnhub made"
}));
}
);
});
ws.on('close', () => {
console.log("websocket closed");
stocks.closeWebSocket();
})
});
app.post("/logIn", function(req, res) {
console.log("signing in...");
(async () => {
let response = await user.logIn(JSON.parse(req.body["user"]));
res.json(response);
})();
})
app.post("/getUserData", function(req, res) {
console.log("getting user data...");
(async () => {
let response = await user.getUserInfo(JSON.parse(req.body["id"]));
res.json(response);
})();
});
app.put("/updateUserData", function(req, res) {
console.log("updating user data...");
(async () => {
let response = await user.updateUserInfo(req.body["id"], req.body["data"]);
res.json(response);
})();
});
app.post("/searchForSymbol", function(req, res) {
console.log("searching for symbol...");
(async () => {
stocks.closeWebSocket();
let symbol = req.body["value"];
let response = await search.searchForSymbol(symbol);
let stockObject = await stocks.loadStock(response);
res.send(stockObject);
})();
});
app.post("/getNews", function(req, res) {
console.log("getting news from stocks...");
(async () => {
let response = await news.getNews(req.body["stocks"], req.body["from"], req.body["to"]);
res.json(response);
})();
});
app.get("/getTopNews", function(req, res) {
console.log("getting top news story...");
(async () => {
let response = await news.getTopNewsStory();
res.json(response);
})();
});