-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (94 loc) · 2.84 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
const { resolve } = require("path");
const http = require("http"),
express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
morgan = require("morgan"),
methodOverride = require("method-override"),
passport = require("passport"),
User = require("./models/user"),
localstrategy = require("passport-local"),
passportlocalmongoose = require("passport-local-mongoose"),
cookieParser = require("cookie-parser"),
socketIO = require("socket.io"),
flash = require("connect-flash");
var imageRouter = require("./routes/image"),
commentRouter = require("./routes/comment"),
authRoutes = require("./routes/auth");
const hostname = "localhost",
port = 3000;
const mongoURI = require("./mongoURI");
app.use(cookieParser());
mongoose.Promise = global.Promise;
app.use(require("express-session")({
secret: "sin cos and tan are basic trigo functions",
resave: false,
saveUninitialized: false,
}));
let gfs;
mongoose.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
});
let conn = mongoose.connection;
conn.once("open", () => {
gfs = new mongoose.mongo.GridFSBucket(conn.db, {
bucketName: "uploads"
});
console.log("Database Connected");
});
conn.on("error", err => {
console.log(`Connection Error: ${err}`);
});
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(morgan("dev"));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localstrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function(req, res, next) {
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
res.locals.currentUser = req.user;
next();
})
app.use(function(req, res, next) {
if (req.path != '/login' && req.session.returnTo) {
delete req.session.returnTo
}
next()
})
app.use('/image/:id/comments', commentRouter);
app.use(authRoutes);
app.use('/image', function(req, res, next) {
req.image_config = {
gfs: gfs
}
next();
}, imageRouter);
app.get("/", function(req, res) {
res.render("photos/landing");
});
const server = http.createServer(app);
server.listen(port, hostname, () => {
console.log(`Server is listening on http://${hostname}:${port}/`);
});
let io = socketIO(server);
io.sockets.on('connection', socket => {
console.log('user connected!');
socket.on('chat', msg => {
socket.broadcast.emit('chat', msg);
})
socket.on('disconnect', () => {
console.log('user disconnected!');
})
})