-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (70 loc) · 2.48 KB
/
index.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
/**
* express - a minimalist framework for node.js
* mongodb - non-relational database
* path - used for handling the file path
* connect-flash and express-session used for displaying error and success messages to the user after filling the form
* passport - for authentication
*/
const express = require('express')
const mongoose = require('mongoose')
const path = require('path')
const passport = require('passport')
const flash = require('connect-flash')
const session = require('express-session')
const app = express()
// specifying the port
const port = 8089
// Passport Config
require('./config/passport')(passport)
// DB Config
const db = require('./config/keys').mongoURI
// Connect to MongoDB
mongoose
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB Connected'))
.catch((err) => console.log(err))
// built in middleware for express to handle incominng post request
app.use(express.urlencoded({ extended: true }))
// Express body parser
app.use(express.json())
// static files configuration for bootstrap, css, jquery and fontawesome icons
app.use(express.static(path.join(__dirname, 'public')))
app.use('/css', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/css')))
app.use('/js', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/js')))
app.use('/js', express.static(path.join(__dirname, 'node_modules/jquery/dist')))
// setting up the view engine
app.set('views', __dirname + '/views')
app.set('view engine', 'ejs')
app.engine('html', require('ejs').renderFile)
// Express session
app.use(
session({
secret: 'secret',
resave: true,
saveUninitialized: true
})
)
// Passport middleware
app.use(passport.initialize())
app.use(passport.session())
// connect flash as a middlewar to show the error and success messages
// app.use(flash())
app.use(flash())
// Global variables
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')
next()
})
app.use(function (req, res, next) {
res.locals.user = req.user
next()
})
app.use('/', require('./routes/userRoutes'))
app.use('/question', require('./routes/queRoutes'))
app.use('/answer', require('./routes/ansRoutes'))
app.use('/comment', require('./routes/commentRoutes'))
app.use('/follow', require('./routes/followRoutes'))
// listening the app on the specified port
app.listen(port, () => console.log(`App is running at port ${port}!`))