-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (73 loc) · 2.55 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
const express = require('express');
const app = new express();
const ejs = require('ejs');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const expressSession = require('express-session');
const flash = require('connect-flash-plus');
const newPostController = require('./controllers/newPost');
const homeController = require('./controllers/home');
const storePostController = require('./controllers/storePost');
const getPostController = require('./controllers/getPost');
const newUserController = require('./controllers/newUser');
const storeUserController = require('./controllers/storeUser');
const loginController = require('./controllers/login');
const loginUserController = require('./controllers/loginUser');
const logoutController = require('./controllers/logout');
const validateMiddleware = require('./middleware/validationMiddleware');
const authMiddleware = require('./middleware/authMiddleware');
const redirectIfAuthenticatedMiddleware = require('./middleware/redirectIfAuthenticatedMiddleware');
let port = process.env.PORT;
if (port == null || port == '') {
port = 4000;
}
app.use(fileUpload());
mongoose.connect(
'mongodb+srv://<admin>:<password>@<mLab-cluster-name>.tjqjs.mongodb.net/<db_name>',
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}
);
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(
expressSession({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true,
// cookie: { maxAge: 6000 },
})
);
app.use(flash());
global.loggedIn = null;
app.use('*', (req, res, next) => {
loggedIn = req.session.userId;
next();
});
app.use('/posts/store', validateMiddleware);
app.get('/posts/new', authMiddleware, newPostController);
app.get('/', homeController);
app.get('/post/:id', getPostController);
app.get('/auth/register', redirectIfAuthenticatedMiddleware, newUserController);
app.get('/auth/login', redirectIfAuthenticatedMiddleware, loginController);
app.get('/auth/logout', logoutController);
app.post('/posts/store', storePostController);
app.post(
'/users/register',
redirectIfAuthenticatedMiddleware,
storeUserController
);
app.post(
'/users/login',
redirectIfAuthenticatedMiddleware,
loginUserController
);
app.use((req, res) => res.render('notfound'));
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: true }));
app.listen(port, () => {
console.log(`App listening on port 🔥`);
});