-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
77 lines (68 loc) · 2.03 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const bodyParser = require('body-parser');
const fs = require('fs');
const cors = require('cors');
const db = require('./db');
const usersComment = require('./models/usersComment');
const getLuckyNumberInText = require('./calculate');
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
app.use(express.static('client'));
const server = http.createServer(app);
const init = async () => {
await Promise.all([
db.init(),
]);
}
const io = socketIO(server);
io.on('connection', (socket) => {
console.log('A user connected');
socket.on('disconnect', () => {
console.log('A user disconnected');
});
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/client/livetream.html');
});
app.post('/api/setData', (req, res) => {
try {
let data = req.body;
const arrayHref = data.location.split('/');
let userData = data.userData;
// console.log({data})
const luckyNumber = getLuckyNumberInText.getLuckyNumberInText(userData?.textMessage || '');
console.log({luckyNumber})
io.emit(arrayHref[3], {
...userData,
luckyNumber,
viewers: data.viewers,
type: data?.type,
});
// usersComment.importMessage({
// ...userData,
// channel: arrayHref[3],
// viewers: data.viewers,
// luckyNumber,
// })
res.send('User created successfully');
// fs.exists(`./data/${arrayHref[3]}.json`, function(exists) {
// fs.writeFileSync(`./data/${arrayHref[3]}.json`, JSON.stringify(data));
// });
} catch (error) {
console.log({error})
}
});
server.listen(3000, () => {
console.log('Server is running on port 3000');
init();
});