-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
125 lines (111 loc) · 3.63 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
// Load envioronment variables
require('dotenv').config();
const express = require('express');
const flash = require('express-flash'); // for showing login error messages
const session = require('express-session'); // for managing user sessions
const mongoose = require('mongoose');
const config = require('./config');
const passport = require('./passport.js');
var path = require('path');
const app = express();
var path = require('path');
app.use(express.urlencoded({ extended: true })); // replaces body-parser
app.use(express.static('public')); // define where static assets live
app.use(express.json()); // parse application/json
app.use(flash());
// Setup Handlebars
const exphbs = require('express-handlebars');
app.engine(
'hbs',
exphbs.engine({
defaultLayout: 'main',
extname: 'hbs',
partialsDir: [path.join(__dirname, 'views/clinician')],
})
);
app.set('View engine', 'hbs'); // set Handlebars view engine
app.use(
session({
// The secret used to sign session cookies (ADD ENV VAR)
secret: process.env.SESSION_SECRET || 'TutelDiabetesHome',
saveUninitialized: true,
resave: true,
proxy: process.env.NODE_ENV === 'production', // to work on Heroku
cookie: {
sameSite: 'strict',
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
maxAge: 24 * 60 * 60 * 1000, // sessions expire after 1 day
},
})
);
//app.use(passport.authenticate('session'));
app.use(passport.initialize());
app.use(passport.session());
const userRoute = require('./routes/user');
const userDataRoute = require('./routes/user-data');
const staticPageRoute = require('./routes/static-page');
const clinicianRoute = require('./routes/clinician');
const patientRoute = require('./routes/patient');
const apiRoute = require('./routes/api');
const port = process.env.PORT || 3000;
const host = process.env.HOST || 'localhost';
app.use(userRoute);
app.use(userDataRoute);
app.use(staticPageRoute);
app.use(clinicianRoute);
app.use(patientRoute);
app.use('/api', apiRoute);
// Tells the app to listen on port 3000 and logs that information to the
app.listen(port, () => {
console.log(`⚡Server is running on ${host}:${port}`);
initMongooseConnection();
});
/** Function to stop connecting the DB and close the app */
function stop(callback) {
mongoose.disconnect();
mongoose.connection.once('close', () => {
server.close(callback);
});
}
/**
* Initialize connection to mongoDB and setup on-event emitters.
* Callback is usually used in test for done()
* @param {function} callback
*/
function initMongooseConnection(callback = () => {}) {
const dbURI = config.dbURI;
var options = {
keepAlive: true,
connectTimeoutMS: 30000,
useNewUrlParser: true,
useUnifiedTopology: true,
};
// Exit on error
mongoose.connection.on('error', (err) => {
console.error(err);
process.exit(1);
});
mongoose.connection.on('connecting', () => {
console.log('🐌Connecting. State🐌 ' + mongoose.connection.readyState); // state 2
});
mongoose.connection.on('connected', () => {
console.log('Connected. State ✔️ ' + mongoose.connection.readyState); // state 1
});
mongoose.connection.on('disconnected', () => {
console.log('Disconnected. State ❌ ' + mongoose.connection.readyState); // state 0
});
// Actual connection part
mongoose.connect(dbURI, options);
var db = mongoose.connection;
db.on('error', (err) => {
console.log('Failed to connect to database');
console.log(err);
process.exit(1);
});
db.once('open', async () => {
console.log(`Mongo connection started on ${db.host}:${db.port}`);
console.log('DB Name: ' + db.name);
callback();
});
}