-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
120 lines (96 loc) · 2.93 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const express = require('express');
const app = express();
const path = require('path');
const expressHbrs = require('express-handlebars');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const upload = require('express-fileupload');
require("dotenv").config();
const {
mongoDbUrl
} = require('./config/database');
const passport = require('passport');
const {
allowInsecurePrototypeAccess
} = require('@handlebars/allow-prototype-access');
const Handlebars = require('handlebars');
//for flash msg
const session = require('express-session');
const flash = require('connect-flash');
mongoose.Promise = global.Promise;
//mongoose connect
mongoose.connect(mongoDbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
}).then(db => console.log("Mongo Connected")).catch(error => console.log("Could not connect" + error));
app.use(express.static(path.join(__dirname, 'public'))); //for using static files
//register handlebars-helpers function
const {
select,
generateDate,
paginate
} = require('./helpers/handlebars-helpers');
//engine
app.engine('handlebars', expressHbrs({
defaultLayout: 'home',
helpers: {
select: select,
generateDate: generateDate,
paginate: paginate
},
handlebars: allowInsecurePrototypeAccess(Handlebars),
}));
app.set('view engine', 'handlebars');
//Upload Middleware
app.use(upload());
//Body Parser
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
//Method Override
app.use(methodOverride('_method'));
//Session
app.use(session({
secret: 'nafisahasan',
resave: true,
saveUninitialized: true
}));
app.use(flash());
//passport
app.use(passport.initialize());
app.use(passport.session());
//set up local variables using middleware for handlebars
app.use((req, res, next) => {
res.locals.user = req.user || null;
res.locals.success_message = req.flash('success_message');
res.locals.error_message = req.flash('error_message');
res.locals.error = req.flash('error');
next();
});
//load Routes
//main routes
//we exports out main routes here
const home = require('./routes/home/index');
//require admin
const admin = require('./routes/admin/index');
const posts = require('./routes/admin/posts');
const categories = require('./routes/admin/categories');
const comments = require('./routes/admin/comments');
const todos = require('./routes/admin/todos');
//Use Routes
//let application know about main.js router by Middleware
app.use('/', home); //all functionality going to be here
app.use('/admin', admin);
app.use('/admin/posts', posts);
app.use('/admin/categories', categories);
app.use('/admin/comments', comments);
app.use('/admin/todos', todos);
//listen to the port
const port = process.env.PORT || 4500;
app.listen(port, () => {
console.log(`Listening to port ${port}`);
});