-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
135 lines (91 loc) · 3.14 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const express = require('express');
const app = express();
const path = require('path');
const handlebars = require('express-handlebars');
const Handlebars = require('handlebars')
const port = process.env.PORT || 1234;
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const {allowInsecurePrototypeAccess} = require('@handlebars/allow-prototype-access');
const methodOverride = require('method-override')
const upload = require('express-fileupload');
const session = require('express-session');
const flash = require('connect-flash')
const passport = require('passport');
// const eq = require('ember-truth-helpers')
const {mongoDbURl} = require('./config/database')
mongoose.set('useFindAndModify', false);
mongoose.Promise = global.Promise;
// mongoose.connect('mongodb://localhost:27017/nodeCms', {useNewUrlParser: true, useUnifiedTopology: true })
mongoose.connect(mongoDbURl, {useNewUrlParser: true, useUnifiedTopology: true })
.then(db=> console.log('Connected'))
.catch(err=> console.log(err))
app.use(express.static(path.join(__dirname, 'public'))) // Loading Static files like css, js and stuffs
// custom select handlebars function
const {select, generate_date, ifeq, paginate} = require('./helpers/handlebars-helpers')
// --SETTING view engine using handlebars
app.engine('handlebars', handlebars(
{
defaultLayout: 'home',
helpers:{select: select, generate_date: generate_date, ifeq: ifeq, paginate: paginate},
partialsDir: path.join(__dirname, "views/layouts/partials"),
handlebars: allowInsecurePrototypeAccess(Handlebars)
}
))
// upload middleware
app.use(upload())
// setting view engine
app.set('view engine', 'handlebars')
//
// Setting up bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//
// Session Middleware
app.use(session({
secret: 'tonyAtuoha',
resave: true,
saveUninitialized: true
}));
// Flash Middleware
app.use( flash() );
//Passport inits
app.use(passport.initialize());
app.use(passport.session());
// sETTing local variable for flash msgs
app.use( (req, res, next)=>{
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.loggedUser = req.user || null
next();
})
// override with POST having ?_method=DELETE
app.use(methodOverride('_method'))
// Routes
// --home
const home = require('./routes/home/main')
app.use('/', home)
// --admin
const admin = require('./routes/admin/main');
app.use('/admin', admin)
//
// -- admin posts
const posts = require('./routes/admin/posts');
app.use('/admin/posts', posts)
//
// -- admin categories
const category = require('./routes/admin/category');
app.use('/admin/categories', category)
// -- admin users
const user = require('./routes/admin/user');
app.use('/admin/users', user);
// -- comments
const comment = require('./routes/admin/comment')
app.use('/comments', comment)
// -- replies
const replies = require('./routes/admin/reply');
app.use('/replies', replies);
app.listen(port, ()=>{
console.log(`listening to port: ${port}`)
})