-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
105 lines (93 loc) · 2.77 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
'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
// const passport = require('passport');
// const LocalStrategy = require('passport-local');
// const bcrypt = require('bcrypt');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const routes = require('./routes.js');
const auth = require('./auth.js');
const passportSocketIo = require('passport.socketio');
const MongoStore = require('connect-mongo')(session);
const cookieParser = require('cookie-parser');
const URI = process.env.MONGO_URI;
const store = new MongoStore({ url: URI });
fccTesting(app); //For FCC testing purposes
app.set('view engine', 'pug');
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
key: 'express.sid',
secret: process.env.SESSION_SECRET,
resave: true,
store: store,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use((req, res, next) => {
res.status(404)
.type('text')
.send('Not Found');
});
myDB(async client => {
const myDataBase = await client.db('database').collection('users');
auth(app, myDataBase);
routes(app, myDataBase);
function onAuthorizeSuccess(data, accept) {
console.log('Successful connection to socket.io');
accept(null, true);
}
function onAuthorizeFail(data, message, error, accept) {
if (error) throw new Error(message);
console.log('Failed connection to socket.io: ', message);
accept(null, false);
}
io.use(
passportSocketIo.authorize({
cookieParser: cookieParser,
key: 'express.sid',
secret: process.env.SESSION_SECRET,
store: store,
success: onAuthorizeSuccess,
fail: onAuthorizeFail
})
);
let currentUsers = 0;
io.on('connection', socket => {
console.log('User ' + socket.request.user.name + ' connected');
++currentUsers;
io.emit('user', {
name: socket.request.user.name,
currentUsers: currentUsers,
connected: true
});
socket.on('disconnect', () => {
--currentUsers;
io.emit('user', {
name: socket.request.user.name,
currentUsers: currentUsers,
connected: false
});
});
socket.on('chat message', message => {
io.emit('chat message', {name: socket.request.user.name, message: message})
});
});
}).catch(e => {
app.route('/').get((req, res) => {
res.render('pug', {
title: e,
message: 'Unable to login'
});
});
});
const PORT = process.env.PORT || 3000;
http.listen(PORT, () => {
console.log('Listening on port ' + PORT);
});