-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (87 loc) · 2.51 KB
/
index.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
//CREATE APP
var express = require("express");
var app = express();
//CREATING THE SOCKET IO SERVER
var server = require('http').createServer(app);
var io = require('socket.io')(server);
//INCLUDE BODY PARSER
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
//INCLUDE AND USE EJS LAYOUTS
var ejsLayouts = require("express-ejs-layouts");
app.use(ejsLayouts);
//INCLUDE REQUEST
var request = require('request');
//INCLUDE EXPRESS SESSION IN APP
var session = require('express-session');
app.use(session({
secret: 'wdipingpong',
resave: false,
saveUninitialized: true
}));
//INCLUDE AND USE FLASH
var flash = require('connect-flash');
app.use(flash());
//INCLUDE THE DATABASE TO BE ACCESSED ON THE PAGE
var db = require('./models');
//INCLUDE AND USE CLOUDINARY AND MULTER
var cloudinary = require("cloudinary");
var multer = require("multer");
var upload = multer({ dest: './uploads/' });
//USE MY STATIC FOLDER
app.use(express.static(__dirname + '/static'));
app.set("view engine", "ejs");
//CREATE GLOBAL CURRENT USER VARIABLE FOR SOCKET TO USE
var currentUser;
app.use(function(req, res, next){
if (req.session.user) {
db.user.findById(req.session.user).then(function(user){
if (user){
req.currentUser = user;
currentUser = req.currentUser;
next();
} else {
req.currentUser = false;
next();
}
})
} else{
req.currentUser = false;
next();
}
});
app.use(function(req, res, next){
res.locals.currentUser = req.currentUser;
res.locals.alerts = req.flash();
next();
});
function processBoth(io){
return function(req, res){
var currentUser = req.currentUser.name;
io.sockets.emit('client message', currentUser);
};
}
app.get("/", function(req, res){
res.render("index");
});
app.get('/morechat', processBoth(io));
// io.on('connection', function(socket){
// console.log( currentUser.name + ' connected');
// });
app.use("/auth", require("./controllers/auth"));
app.use("/profile", require("./controllers/profile"));
app.use("/chatroom", require("./controllers/chatroom"));
io.on('connection', function(socket){
console.log("connected");
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('message', function(data){
console.log('message received');
io.emit('message', data);
})
});
// app.use("/input", require("./controllers/input"));
// app.use("/players", require("./controllers/players"));
// app.use("/getbetter", require("./controllers/getbetter"));
server.listen(process.env.PORT || 3000);