-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
147 lines (131 loc) · 4.72 KB
/
server.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require('dotenv').config();
const hbs = require('express-handlebars');
const express = require('express');
const session = require('express-session');
const { urlencoded } = require('express');
const path=require('path');
const app = express();
//Use Session
app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: 'secret-key-123',
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: 30*24*60*60*1000},
}))
app.use(function(req,res,next){
res.locals.session=req.session;
next();
})
//Router and model
const RegisterRouter=require('./routers/register.r');
const LoginRouter=require('./routers/login.r');
const LogoutRouter=require('./routers/logout.r');
const SearchRouter=require('./routers/search.r');
const DetaiDrugRouter=require('./routers/detail-drug.r');
const ProfileRouter=require('./routers/profile.r');
const DetailDoctorRouter=require('./routers/detail-doctor.r');
const DocumentRouter=require('./routers/doc.r');
const EditRouter=require('./routers/edit.r');
const doctorM=require('./model/Doctors.m');
//Use static resources
app.use(express.static(path.join(__dirname, '/public')))
app.use(express.urlencoded({
extended: true
}))
app.use(express.json())
//Template engine
app.engine('hbs', hbs.engine({
extname: 'hbs',
defaultLayout: 'main',
layoutsDir: path.join(__dirname, '/views/layouts'),
partialsDir: [
path.join(__dirname, '/views/partials')
],
helpers: {
ifEquals: function(a, b, options) {
if (a === b) {
return options.fn(this);
}
return options.inverse(this);
},
sum: function(a, b) {
return a+b;
},
createInput: function(n) {
var listRow = '';
for(var i = 0; i < n; ++i)
{
listRow +=
`
<tr>
<th scope="row" class="text-center">${i + 1}</th>
<td class="text-center"><input type="number" class="text-center" name="sobenhnhan-${i + 1}" min="0"></td>
<td class="text-center"><input type="number" class="text-center" name="doanhthu-${i + 1}" min="0"></td>
<td class="text-center"><input type="text" class="text-center" id="tyle-${i + 1}" name="tyle-${i + 1}" required onkeydown="return false;" style="caret-color: transparent !important;" autocomplete="off"></td>
</tr>
`;
}
return listRow;
},
createOutput: function(n) {
var listRow = '';
for(var i = 0; i < n; ++i)
{
listRow +=
`
<tr>
<th scope="row" class="text-center">${i + 1}</th>
<td class="text-center"><input type="number" class="text-center" name="sobenhnhan-${i + 1}" value="{{data.sobenhnhan-${i + 1}}}" min="0"></td>
<td class="text-center"><input type="number" class="text-center" name="doanhthu-${i + 1}" value="{{data.doanhthu-${i + 1}}}" min="0"></td>
<td class="text-center"><input type="text" class="text-center" id="tyle-${i + 1}" name="tyle-${i + 1}" value="{{data.tyle-${i + 1}}}" required onkeydown="return false;" style="caret-color: transparent !important;" autocomplete="off"></td>
</tr>
`;
}
return listRow;
},
indexOf: function(context,ndx) {
return context[ndx];
}
}
}));
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, '/views'));
//Route
app.use('/dang-ky-tai-khoan',RegisterRouter);
app.use('/dang-nhap',LoginRouter);
app.use('/dang-xuat',LogoutRouter);
app.use('/tim-kiem',SearchRouter);
app.use('/thuoc',DetaiDrugRouter);
app.use('/tai-khoan',ProfileRouter);
app.use('/bac-si',DetailDoctorRouter);
app.use('/tai-lieu',DocumentRouter);
app.use('/chinh-sua',EditRouter);
app.get('/', async(req, res, next) => {
const doctors=await doctorM.getAll();
let role="patient";
if (req.session.Doctor) {
role="doctor";
}
if (req.session.Username) {
res.render('home', { display1: "d-none", display2: "d-block", doctors:doctors, role:role});
}
else {
res.render('home', { display1: "d-block", display2: "d-none", doctors:doctors, role:role});
}
});
app.all('*',function(req, res) {
let role="patient";
if (req.session.Doctor) {
role="doctor";
}
if (req.session.Username) {
res.render('page-not-found', { display1: "d-none", display2: "d-block", role:role});
}
else {
res.render('page-not-found', { display1: "d-block", display2: "d-none", role:role});
}
});
module.exports = app;