-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
131 lines (110 loc) · 3.92 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
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
var express = require("express"),
app = express(),
session = require('express-session'),
request = require("request"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
Campground = require("./models/campground"),
Comment = require("./models/comment"),
seedDB = require("./seed"),
passport = require("passport"),
User = require("./models/user"),
LocalStrategy = require("passport-local"),
flash = require("connect-flash"),
methodOverride = require("method-override"),
cookieParser = require('cookie-parser'),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
var users = [];
var connections = [];
var commentRoutes = require("./routes/comments"),
campgroundRoutes = require("./routes/campgrounds"),
chatroomRoutes = require("./routes/chatroom"),
reviewRoutes = require("./routes/reviews"),
indexRoutes = require("./routes/index");
console.log(process.env.DATABASEURL);
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
//seedDB();
var url = process.env.DATABASEURL || "mongodb://localhost/yelp_camp"
mongoose.connect(url);
//mongoose.connect("mongodb://localhost/yelp_camp");
//mongoose.connect("mongodb://shaobo:[email protected]:31963/yelpcampshao");
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.set("view engine", "ejs");
//app session
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
//passport config
app.use(require("express-session")({
secret: "once again song fang is cutiest",
resave: false,
saveUnitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//all locals can have currentUser
app.use(async function(req, res, next){
res.locals.currentUser = req.user;
if(req.user) {
try {
let user = await User.findById(req.user._id).populate('notifications', null, { isRead: false }).exec();
res.locals.notifications = user.notifications.reverse();
} catch(err) {
console.log(err.message);
}
}
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
//use/require the routes
app.use(indexRoutes);
app.use("/album/:id/comments",commentRoutes);
app.use("/album",campgroundRoutes);
app.use("/chatroom",chatroomRoutes);
app.use("/album/:id/reviews", reviewRoutes);
io.on('connection', function(socket){
connections.push(socket);
console.log('Connected: %s sockets connected', connections.length);
//disconnect
socket.on('disconnect', function(data){
// if (!socket.username) {
// return;
// }
users.splice(users.indexOf(socket.username), 1);
updateUsernames();
connections.splice(connections.indexOf(socket), 1);
console.log('Disconnected: %s sockets connected', connections.length);
});
//send message
socket.on('send message', function(data){
//console.log(data.name);
//console.log(req.cookie);
io.sockets.emit('new message', {msg: data.msg, name: socket.username});
});
//new user
socket.on('new user', function(data, callback){
callback(true);
socket.username = data;
users.push(socket.username);
updateUsernames();
});
function updateUsernames() {
io.sockets.emit('get users', users);
}
});
server.listen(process.env.PORT, process.env.IP, function(){
console.log("YelpCamp server on!");
});
//every exprees need this line at bottom