-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (73 loc) · 2.51 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
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const session = require('express-session')
const passport = require("passport");
const SequelizeStore = require("connect-session-sequelize")(session.Store);
const db = require("./models");
const sessionStore = new SequelizeStore({ db });
const socketio = require('socket.io')
const PORT = process.env.PORT || 8080;
passport.serializeUser((user, done) => done(null, user.id))
passport.deserializeUser((id, done) =>
db.models.helper.findById(id)
.then(user => {
if(user) done(null, user)
else {
db.models.helpee.findById(id)
.then(user2 => {
done(null, user2)
})
.catch(done)
}
})
.catch(done))
//import routes
const helpeeRouter = require("./server/routes/helpee");
const helperRouter = require("./server/routes/helper");
const loginRouter = require("./server/routes/login");
const registerRouter = require("./server/routes/register");
const authRouter = require("./server/routes/auth");
const jobsRouter = require("./server/routes/jobs");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//passport auth
app.use(session({
secret: process.env.SESSION_SECRET || 'my best friend is Cody',
store: sessionStore,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 1000 * 60 * 60 * 24 }
}))
app.use(passport.initialize())
app.use(passport.session())
app.use("/auth", authRouter);
//register routes
app.use("/helpee", helpeeRouter);
app.use("/helper", helperRouter);
app.use("/login", loginRouter);
app.use("/register", registerRouter);
app.use("/jobs", jobsRouter);
//serve static
app.use(express.static(path.join(__dirname, "public")));
app.get('/', function (req, res, next) {
res.sendFile(path.join(__dirname, 'public/register.html'));
});
app.get('/map', function (req, res, next) {
req.user ? res.sendFile(path.join(__dirname, 'public/map.html')) :
res.sendFile(path.join(__dirname, 'public/register.html'));
})
app.use(function(err, req, res, next) {
console.error(err, err.stack);
res.status(500).send(err);
});
sessionStore.sync()
.then(() => {
db.sync({ force: true }).then(() => {
const server = app.listen(PORT, () => console.log(`Helping seniors on port ${PORT}`));
const io = socketio(server)
require('./server/socket')(io)
});
})
module.exports = app;