-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
125 lines (102 loc) · 3.18 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
const express = require('express')
const app = express()
const mysql = require('mysql')
/**
* This middleware provides a consistent API
* for MySQL connections during request/response life cycle
*/
const myConnection = require('express-myconnection')
/**
* Store database credentials in a separate config.js file
* Load the file/module and its values
*/
const config = require('./config')
const dbOptions = {
host: config.database.host,
user: config.database.user,
password: config.database.password,
port: config.database.port,
database: config.database.db
}
/**
* 3 strategies can be used
* single: Creates single database connection which is never closed.
* pool: Creates pool of connections. Connection is auto release when response ends.
* request: Creates new connection per new request. Connection is auto close when response ends.
*/
app.use(myConnection(mysql, dbOptions, 'pool'))
/**
* setting up the templating view engine
*/
app.set('view engine', 'ejs')
/**
* import routes/index.js
* import routes/question.js
*/
const index = require('./routes/index')
const question = require('./routes/question')
const admin = require('./routes/admin')
app.use(express.static(__dirname + '/public'));
/**
* Express Validator Middleware for Form Validation
*/
const expressValidator = require('express-validator')
app.use(expressValidator())
/**
* body-parser module is used to read HTTP POST data
* it's an express middleware that reads form's input
* and store it as javascript object
*/
const bodyParser = require('body-parser')
/**
* bodyParser.urlencoded() parses the text as URL encoded data
* (which is how browsers tend to send form data from regular forms set to POST)
* and exposes the resulting object (containing the keys and values) on req.body.
*/
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
/**
* This module let us use HTTP verbs such as PUT or DELETE
* in places where they are not supported
*/
const methodOverride = require('method-override')
/**
* using custom logic to override method
*
* there are other ways of overriding as well
* like using header & using query value
*/
app.use(methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method
delete req.body._method
return method
}
}))
/**
* This module shows flash messages
* generally used to show success or error messages
*
* Flash messages are stored in session
* So, we also have to install and use
* cookie-parser & session modules
*/
var flash = require('express-flash')
var cookieParser = require('cookie-parser');
var session = require('express-session');
// app.use(session({secret: 'ssshhhhh',saveUninitialized: true,resave: true}));
app.use(cookieParser('keyboard cat'))
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
// cookie: { maxAge: 60000 }
}))
app.use(flash())
app.use('/', index)
app.use('/question', question)
app.use('/admin', admin)
app.listen(3000, function(){
console.log('Server running at port 3000: http://127.0.0.1:3000')
})