-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (34 loc) · 1.14 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
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
var listOfStockData = [];
app.use(bodyParser.json());
app.use(express.static(path.resolve(__dirname, 'views/dist')));
io.on('connection', (socket) => {
// console.log("User connected");
socket.on('disconnect', () => {
// console.log('User disconnected');
});
socket.on('add-stock', (data) => {
listOfStockData.push(data);
io.emit('stockAdded', {stockData: data});
});
socket.on('remove-stock', (index) => {
listOfStockData.splice(index, 1);
io.emit('stockRemoved', {stockIndex: index});
});
});
app.get('/getStocks', (req, res) => {
res.send(listOfStockData);
})
app.get('/*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'views/dist/index.html'));
})
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", () => {
var addr = server.address();
console.log("Server listening at port", addr.address + ":" + addr.port);
});