-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (42 loc) · 1.31 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
const express = require('express');
const authRouter = require('./routes/auth-routes');
const cardRouter = require('./routes/card-routes');
const passportSetup = require('./config/passport-setup');
const mongoose = require('mongoose');
const keys = require('./config/keys');
const cookieSession = require('cookie-session');
const passport = require('passport');
const Card = require('./models/card-model');
const PORT = 3333;
const app = express();
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(express.json());
app.use(cookieSession({
maxAge: 24 * 60 * 60 * 1000,
keys: [keys.session.cookieKey]
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect('mongodb://localhost:27017/whistle',
{useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true}, () => {
console.log('connected to mongodb');
});
app.use('/auth', authRouter);
app.use('/card', cardRouter);
const authCheck = (req, res, next) => {
if (!req.user) {
res.redirect('/auth/login');
} else {
next();
}
}
app.get('/', authCheck, (req, res) => {
Card.find().then(cards => {
cards.reverse();
res.render('index.ejs', {user: req.user});
});
});
app.listen(PORT, () => {
console.log(`app listening on port ${PORT}`);
});