-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (64 loc) · 2.21 KB
/
index.js
File metadata and controls
86 lines (64 loc) · 2.21 KB
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
// import module `express`
const express = require('express');
// import module `hbs`
const hbs = require('hbs');
// import module `express-session`
const session = require('express-session');
// import module `mongoose`
const mongoose = require('mongoose');
// import module `connect-mongo`
const MongoStore = require('connect-mongo')(session);
// import module `routes` from `./routes/routes.js`
const routes = require('./routes/routes.js');
// import module `database` from `./model/db.js`
const db = require('./models/db.js');
const app = express();
const port = 9090;
// set `hbs` as view engine
app.set('view engine', 'hbs');
// sets `/views/partials` as folder containing partial hbs files
hbs.registerPartials(__dirname + '/views/partials');
// parses incoming requests with urlencoded payloads
app.use(express.urlencoded({extended: true}));
// set the folder `public` as folder containing static assets
// such as css, js, and image files
app.use(express.static('public'));
// connects to the database
db.connect();
// use `express-session`` middleware and set its options
// use `MongoStore` as server-side session storage
app.use(session({
'secret': 'ccapdev-session',
'resave': false,
'saveUninitialized': false,
store: new MongoStore({mongooseConnection: mongoose.connection})
}));
// define the paths contained in `./routes/routes.js`
app.use('/', routes);
// if the route is not defined in the server, render `../views/error.hbs`
// always define this as the last middleware
app.use(function (req, res) {
var details = {};
/*
checks if a user is logged-in by checking the session data
if a user is logged-in,
display the profile tab and logout tab in the nav bar.
*/
if(req.session.idNum) {
details.flag = true;
details.name = req.session.name;
details.idNum = req.session.idNum;
}
/*
if no user is logged-in,
do not display the profile tab and the logout tab in the nav bar.
*/
else
details.flag = false;
// render `../views/error.hbs`
res.render('error', details);
});
// binds the server to a specific port
app.listen(port, function () {
console.log('app listening at port ' + port);
});