-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (60 loc) · 2.07 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
// import module `express`
const express = require('express');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const mongoose = require('mongoose');
mongoose.connect('mongodb+srv://MauriesLopez:[email protected]/?retryWrites=true&w=majority');
// import module `hbs`
const hbs = require('hbs');
// 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 db = mongoose.connection;
const app = express();
const port = 3000;
app.use(session({
secret: 'sessionID',
cookie: {maxAge: 7*24*60*60*3000},
saveUninitialized: false,
store: new MongoStore({mongooseConnection: db, autoRemove: 'disabled'}),
resave: false
}));
// set `hbs` as view engine
app.set('view engine', 'hbs');
app.use(express.json());
// 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'));
// 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.idNumber) {
details.flag = true;
details.idNumber = req.session.idNumber;
}
/*
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);
});
// connects to the database
//db.connect();
// binds the server to a specific port
app.listen(port, function () {
console.log('app listening at port ' + port);
});