-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
123 lines (99 loc) · 3.74 KB
/
app.js
File metadata and controls
123 lines (99 loc) · 3.74 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
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
import express from "express";
import mongoose from "mongoose";
import cors from 'cors';
import exphbs from 'express-handlebars';
import dotenv from "dotenv";
import flash from 'express-flash';
import session from 'express-session';
import Handlebars from 'handlebars';
import path from "path";
import {allowInsecurePrototypeAccess} from '@handlebars/allow-prototype-access';
if (process.env.NODE_ENV !== 'production') {
dotenv.config()
}
import glucoseRouter from "./Routes/Glucose.js";
import patientRouter from "./Routes/patientRouter.js";
import clinicianRouter from "./Routes/clinician.js";
const __dirname = path.resolve();
const app = express();
app.use(express.static('public'))
app.use(express.json({ limit: '30mb', extended: true }))
app.use(express.urlencoded({ limit: '30mb', extended: true }))
app.use(cors());
app.use("/images", express.static(path.join(__dirname, "/public/assets")));
app.use("/", glucoseRouter);
app.use("/", clinicianRouter);
app.engine('hbs', exphbs.engine({
handlebars: allowInsecurePrototypeAccess(Handlebars), // configure Handlebars
defaultlayout: 'main',
extname: 'hbs',
helpers :{
isGreater: (x,y) => x > y,
isLess: (x,y) => x < y,
equalString: (s1,s2) => s1 == s2,
// Try to find the most recent data. Using helper function
// this is just for testing
dateToday: () => {
const today = new Date()
return today.getDate();
},
returnFalse: () => false,
//checks if data was entered today
enteredToday: (date) => {
const today = new Date()
// checking month for now becuase time zone is messing up the date
return date.getDate() == today.getDate() &&
date.getMonth() == today.getMonth() &&
date.getFullYear() == today.getFullYear();
},
getEngRate: (pId, dateRegistered) => {
const msInDay = 1000 * 60 * 60 * 24
const today = new Date()
const diff = today - dateRegistered
const daysRegisteredFor = Math.floor(diff / msInDay)
//const allDates = glucoseModel.find({patientId: pId}, {dateTime: true} ).distinct().count()
return daysRegisteredFor;
//return daysRegisteredFor / allDates * 100;
}
}
}))
// set Handlebars view engine
app.set('view engine', 'hbs')
// Flash messages for failed logins, and (possibly) other success/error messages
app.use(flash())
// Track authenticated users through login sessions
app.use(
session({
// The secret used to sign session cookies (ADD ENV VAR)
secret: process.env.SESSION_SECRET || 'sugardoctor123',
name: 'SugarDoctor', // The cookie name (CHANGE THIS)
saveUninitialized: false,
resave: false,
proxy: process.env.NODE_ENV === 'production',
cookie: {
sameSite: 'strict',
httpOnly: true,
secure: app.get('env') === 'production',
maxAge: 24 * 60 * 60 * 1000
},
})
)
if (app.get('env') === 'production') {
app.set('trust proxy', 1); // Trust first proxy
}
// Initialise Passport.js
import passport from "./passport.js";
app.use(passport.authenticate('session'))
// Load authentication router
import authRouter from "./Routes/auth.js";
app.use(authRouter)
app.use(express.static('public'))
// send HTTP requests to router
app.use('/', patientRouter)
//const CONNECTION_URL = process.env.CONNECTION_URL;
const CONNECTION_URL = 'mongodb+srv://WebInfoTech:Webinfotech@cluster0.r8yef.mongodb.net/WebInfoTech?retryWrites=true&w=majority'
const PORT = process.env.PORT|| 5000;
mongoose.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => app.listen(PORT, () => console.log(`Server Running on Port: http://localhost:${PORT}`)))
.catch((error) => console.log(`${error} did not connect`));
//mongoose.set('useFindAndModify', false);