-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
108 lines (85 loc) · 2.71 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
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
if (process.env.NODE_ENV != 'production') {
require('dotenv').config();
}
const express = require('express');
const http = require('http');
const path = require('path');
const socketIO = require('socket.io');
const fs = require('fs');
const formidable = require('formidable');
const app = express();
let server = http.createServer(app);
let io = socketIO(server);
const Video = require('./models/Video');
var bodyParser = require('body-parser');
const Room = require('./models/Room');
//multer is a middleware used to parse multipart/form-data
const multer = require('multer');
const { storage, cloudinary } = require('./cloudinary');
const upload = multer({ storage });
require('./mongooseConnection');
const port = process.env.PORT || 3000;
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.set('view engine', 'ejs');
//path.join() method joins the specified path segments into one path
const publicPath = path.join(__dirname, '/public');
//To serve static files such as images, CSS files, and JavaScript files
app.use(express.static(publicPath));
// Requiring routes with io param
const youtubeRoute = require('./routes/youtube')(app, io);
const customRoute = require('./routes/custom')(app, io, publicPath);
// setting up view engine
app.set('view engine', 'ejs');
// Main Routing starts
app.use('/youtube', youtubeRoute);
app.use('/custom', customRoute);
app.get('/', (req, res) => {
res.render('details');
});
app.post('/', async (req, res) => {
var name = req.body.name;
Room.findOne({ name: name }, async (err, foundRoom) => {
if (!foundRoom) {
const room = new Room();
room.name = req.body.name;
await room.save();
console.log('room saved');
res.redirect('/landing/' + room._id);
} else {
res.redirect('/landing/' + foundRoom._id);
}
});
});
app.get('/landing/:roomid', (req, res) => {
var roomId = req.params.roomid;
res.render('landing', { roomId });
});
app.post('/landing/:roomid', async (req, res) => {
new formidable.IncomingForm().parse(req).on('file', function (name, file) {
cloudinary.uploader.upload(
file.path,
{ resource_type: 'video', public_id: file.name },
async (error, result) => {
const video = new Video();
video.originalname = result.public_id;
video.size = result.bytes;
video.url = result.url;
video.createdIn = req.params.roomid;
await video.save();
}
);
});
});
io.on('connection', (socket) => {
console.log('success');
});
app.get('/local', (req, res) => {
res.render('localVideo');
});
app.get('/watch', (req, res) => {
res.render('watch');
});
server.listen(port, () => {
console.log('Server running');
});