-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
74 lines (64 loc) · 2.15 KB
/
server.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
const express = require('express');
const app = express();
const controllers = require('./controllers')
const methodOverride = require('method-override');
const session = require("express-session");
const MongoStore = require("connect-mongo");
const { Blog, Content, User } = require('./models');
require('./config/db.connection')
const PORT = process.env.PORT || 4000
app.set('view engine', 'ejs');
app.use(
session(
{
store: MongoStore.create({ mongoUrl: process.env.MONGO_URI }),
secret: "super secret",
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7 * 2 // two weeks
},
}
)
);
app.use(express.urlencoded({ extended: false }));
app.use(express.static('public'));
app.use(methodOverride('_method'));
app.use('/blog', controllers.blog);
app.use('/content', controllers.content)
app.use('/user', controllers.user)
app.get('/', (req, res) => {
res.render('index.ejs');
});
app.get('/dashboard', async function (req, res, next) {
try {
const foundContent = await Content.find({})
const foundUsers = await User.find({})
const foundUser = await User.find({currentSession: req.sessionID})
if (!foundUser) return res.redirect('/user/login')
console.log("Hey hey, the current user is " + foundUser[0].userName)
const foundBlogs = await Blog.find({});
const foundBlog = await Blog.findOne({_id: foundUser[0].blog})
if (!foundBlog) return res.redirect('/blog/blog-creation')
console.log("The current user's blog: " + foundBlog.title)
const context = {
content: foundContent,
allUsers: foundUsers,
currentUser: foundUser,
allBlogs: foundBlogs,
currentBlog: foundBlog
}
res.render('dashboard.ejs', context)
} catch (err) {
console.log(err);
res.send(err);
return next();
}
})
app.get("/*", (req, res) => {
const context = {error: req.error};
return res.status(404).render("404", context);
});
app.listen(PORT, function() {
console.log(`I am listening on ${PORT}`)
});