-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
193 lines (144 loc) · 4.28 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
require('dotenv').config();
var express = require('express');
var ejs = require('ejs');
var port = process.env.PORT || 3000;
var path = require('path');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
//bring in method override
const methodOverride = require('method-override');
// blogs
const blogRouter = require('./routes/blogs');
const Blog = require('./models/Blog');
// battleground
const battlegroundRouter = require('./routes/battleground');
const battlegroundSocket = require('./battleground/socketEventSetup');
// Mongoos connection
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/ManualAuth', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
// Http and Socket.io connection
const http = require('http').Server(app);
const io = require('socket.io')(http);
// must use cookieParser before expressSession
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(require('express').static(path.join(__dirname, 'public')));
////
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
});
app.use(session({
secret: 'work hard',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride('_method'));
app.use(express.static(__dirname + '/views'));
app.use(express.static("static"));
app.use(express.static("public"));
var index = require('./routes/index');
app.use('/', index);
//route for the index
app.get('/community', (req, res, next) => {
console.log("profile");
User.findOne({ unique_id: req.session.userId }, async (err, data) => {
console.log("data");
console.log(data);
if (!data) {
res.redirect('/register');
} else {
//console.log("found");
req.session.data = data
let blogs = await Blog.find().sort({ timeCreated: 'desc' });
res.render('community', { blogs: blogs, data: data });
}
});
});
//app.get('/community', async (request, response) => {
//let blogs = await Blog.find().sort({ timeCreated: 'desc' });
//response.render('community', { blogs: blogs });
//});
//listen to routes
app.use('/blogs', blogRouter);
// ROUTES
app.get('/', function (req, res) {
res.render('home');
});
app.get('/home', function (req, res) {
res.render('home');
});
//app.get('/community', function (req, res) {
//res.render('community');
//});
app.get('/contact', function (req, res) {
res.render('contact');
});
app.get('/drawboard', function (req, res) {
res.render('drawboard');
});
// video_landing
app.get('/experts', function (req, res) {
res.render('expert_landing');
});
// room1
app.get('/expert_session1', function (req, res) {
res.render('room1');
});
// room 2
app.get('/expert_session2', function (req, res) {
res.render('room2');
});
// room 3
app.get('/expert_session3', function (req, res) {
res.render('room3');
});
app.get('/profile', function (req, res) {
var user = req.data
console.log(req.session.userId)
console.log(req.session.data)
//console.log(req)
res.render('profile', { title: 'Profile Page', data: req.session.data });
});
app.get('/sss', function (req, res) {
var user = req.data
//console.log(req)
console.log(req.body)
//console.log(req)
res.render('sss', { title: 'Image Page', data: req.session.data });
});
app.use('/battleground', (req, res, next) => {
battlegroundSocket.initSocketIoCallbacks(io)
next()
}, battlegroundRouter)
// catch 404 and forward to error handler
app.use((req, res, next) => {
var err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
// define as the last app.use callback
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.send(err.message);
});
// listen on port 3000
console.log("Listening to " + port);
http.listen(port, process.env.IP);