-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (43 loc) · 1.58 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
require('dotenv').config();
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const middleware = require('./middleware');
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('./database');
const session = require('express-session');
app.set('view engine', 'pug');
app.set('views', 'views');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use(session({
secret: "sign session",
resave: true,
saveUninitialized: false
}))
// Routes
const loginRoute = require('./routes/loginRoutes');
const registerRoute = require('./routes/registerRoutes');
const postRoute = require('./routes/postRoutes');
const profileRoute = require('./routes/profileRoutes');
const logoutRoute = require('./routes/logout');
// Api routes
const postsApiRoute = require('./routes/api/posts');
const usersApiRoute = require('./routes/api/users');
app.use('/login', loginRoute);
app.use('/register', registerRoute);
app.use("/posts", middleware.requireLogin, postRoute);
app.use("/profile", middleware.requireLogin, profileRoute);
app.use('/logout', logoutRoute);
app.use('/api/posts', postsApiRoute);
app.use('/api/users', usersApiRoute);
app.get('/', middleware.requireLogin, (req, res, next)=>{
var payload = {
pageTitle: 'Home',
userLoggedIn: req.session.user,
userLoggedInJs: JSON.stringify(req.session.user)
}
res.status(200).render('home', payload);
})
app.listen(port, ()=> console.log('Server is running on port:', port));