-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
63 lines (55 loc) · 1.64 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
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 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) => {
let data = req.body;
let userData = data.userData;
const arrayHref = data.location.split('/');
// console.log({data})
io.emit(arrayHref[3], data);
usersComment.importMessage({
...userData,
channel: arrayHref[3],
viewers: data.viewers
})
res.send('User created successfully');
// fs.exists(`./data/${arrayHref[3]}.json`, function(exists) {
// fs.writeFileSync(`./data/${arrayHref[3]}.json`, JSON.stringify(data));
// });
});
server.listen(3000, () => {
console.log('Server is running on port 3000');
init();
});