-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (52 loc) · 2.13 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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const db = require('./db').mongoose;
// helper function
const getSymbols = require('./helpers/getSymbols');
const currentState = require('./helpers/currentState');
const saveLayout = require('./helpers/saveLayout');
const getStatsURL = require('./helpers/getStatsURL');
const getCompanyStats = require('./helpers/getCompanyStats');
const sendCompanyStats = require('./helpers/sendCompanyStats');
const getCompanyList = require('./helpers/getCompanyList');
const getCompanyInfo = require('./helpers/getCompanyInfo');
// Middlewares
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Headers", "Content-Type");
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
next();
});
// Routes
app.get('/', function(req, res) {
res.send('Hello World');
});
app.get('/api/getSymbols', getSymbols);
app.get('/api/currentState', currentState);
app.get('/api/getStatsURL', getStatsURL);
app.get('/api/getCompanyStats', getCompanyStats);
app.get('/api/sendCompanyStats', sendCompanyStats);
app.get('/api/getCompanyList', getCompanyList);
app.post('/api/getCompanyInfo', getCompanyInfo);
app.post('/api/saveLayout', saveLayout);
// once socket is open
io.on('connection', function(socket) {
socket.on('disconnect', function() {
console.log('user disconnected!');
});
socket.on('sample_message', function(val) {
// console.log('Before emit: ', val);
io.emit('new_sample_message',val);
});
socket.on('cell_value_changed', function(val) {
// console.log('Before cell value changed ', date.getHours(), ' ' , val);
io.emit('new_cell_value', val);
})
});
http.listen(3000, () => console.log('Server is connected on 3000!'))