-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
84 lines (79 loc) · 2.69 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
// Loading modules
const path = require('path')
const express = require('express')
const app = express()
const exphbs = require('express-handlebars')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const flash = require('connect-flash')
const session = require('express-session')
const methodOverride = require('method-override')
const passport = require("passport")
const cors = require("cors")
require('./config/auth')(passport)
require('./models/Course')
const Course = mongoose.model('courses')
// App config
// Cors
app.use(cors())
// Session and Flash setup
app.use(session({
secret: 'a4f8071f-c873-4447-8ee2',
resave: true,
saveUninitialized: true
}))
// Passport config
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
// Global Vars
app.use(function(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.user = req.user || null;
next();
})
// Method override configs
app.use(methodOverride('_method'))
// Body parser config
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
// Template engine config
app.engine('handlebars', exphbs({defaultLayout: 'main'}))
app.set('view engine', 'handlebars');
//Static folder config
app.use(express.static(path.join(__dirname,'public')))
// Mongoose ODM config
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/fsocietyead-dev', {
useMongoClient: true
}).then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err))
// Routes
// Main Routes
app.get('/', (req, res) => {
Course.find({}).populate('author').sort('desc').then(courses =>{
res.render("index",{courses: courses})
})
})
// Admin Routes
const admin = require('./routes/admin')
app.use('/admin', admin)
// Categories Routes
// Users Routes
const users = require('./routes/users')
app.use('/users', users)
// Courses Routes
const courses = require('./routes/courses')
app.use('/courses', courses)
// Run Server
// Error 404 Page
app.use(function(req,res){
res.render('404');
});
const PORT = 3098
app.listen(PORT, (err) => {
if(err) throw err;
console.log("UP And Running!")
})