generated from comp426-2022-spring/a99
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
233 lines (191 loc) · 7.92 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const express = require('express')
const args = require("minimist")(process.argv.slice(2))
args["port"]
const app = express()
var accesslogdb = require('./src/accesslogdb.js')
var userdb = require('./src/userdb.js')
const path = require('path')
// Serve static HTML files
app.use(express.static('./public'));
// Allow json body messages
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
// Add cors dependency
const cors = require('cors')
// Set up cors middleware on all endpoints
app.use(cors())
var port = args.port || process.env.PORT || 3000
//creates log/sign up inserts
const logdb = ((req, res, next) => {
let logdata = {
username: req.username,
password: req.password,
time: Date.now(),
}
const stmt = accesslogdb.prepare('INSERT INTO accesslog (username, password, time) VALUES (?, ?, ?)')
const info = stmt.run(logdata.username, logdata.password, logdata.time)
next()
});
const newuserdb = ((req, res, next) => {
let userdata = {
username: req.username,
password: req.pasword,
time: Date.now(),
}
const stmt = userdb.prepare('INSERT INTO userlist (username, password, time) VALUES (?, ?, ?)')
const info = stmt.run(userdata.username, userdata.password, userdata.time)
next()
});
var physical_tasks = ["Walk 6k steps", "\n30 minutes of aerobics", "\n20 minutes of Yoga", "\nGo on a quick jog", "\n10 minutes of stretching", "\n15 minutes of biking"]
var physical_descriptions = ["Walking 6k steps a day is necessary to ensure the health of your joints and muscles!",
"30 minutes of aerobics is the heart's best treatment. It increases your stamina and ensures healthy arteries",
"20 minutes of yoga will improve your flexibility, reduce inflammation, and increase your strength",
"Going on a quick jog is the best way to keep a strong immune system and reduce stress hormones",
"10 minutes of stretching is enough to keep muscles, joints, and bones in their proper alignments",
"15 minutes of biking is perfect for mobility, balance, flexibility, and strength"]
var mental_tasks = ["Spend time outside", "\n30 minutes of reading", "\nTalk to a loved one on the phone", "\n10 minutes of journaling", "\n1 hour without your phone", "\nWatch a 20 minute show"]
var mental_descriptions = ["Spending time outside can help to reduce anxiety and depression. Take a break today and spend some time outside",
"Reading can help improve your quality of sleep and sharpen you mind.",
"Talking to loved ones daily can help increase your sense of belonging and boost your happiness.",
"Journaling helps you to express your thoughts and emotions while giving you a moment of reflection.",
"Give your brain a rest. Unplugging for an hour will help reduce stress and anxiety.",
"Take a moment to relax and watch an episode of your favorite TV show!"]
function randomInRange(min, max){
return Math.floor(Math.random() * (max - min + 1) + min)
}
function date_Randomizer() {
var time = new Date()
const date = time.getDate()
if (date%2==0) {
var task1 = physical_tasks.at(0)
var task2 = physical_tasks.at(2)
var task3 = physical_tasks.at(4)
return [task1, task2, task3]
} else {
var task1 = physical_tasks.at(1)
var task2 = physical_tasks.at(3)
var task3 = physical_tasks.at(5)
return [task1, task2, task3]
}
}
function physicalDescription_Randomizer() {
var time = new Date()
const date = time.getDate()
if (date%2==0) {
var task1 = physical_descriptions.at(0)
var task2 = physical_descriptions.at(2)
var task3 = physical_descriptions.at(4)
return [task1, task2, task3]
} else {
var task1 = physical_descriptions.at(1)
var task2 = physical_descriptions.at(3)
var task3 = physical_descriptions.at(5)
return [task1, task2, task3]
}
}
function randomiz_mental() {
var time = new Date()
const date = time.getDate()
if (date%2 == 0) {
var task1 = mental_tasks.at(0)
var task2 = mental_tasks.at(2)
var task3 = mental_tasks.at(4)
return [task1, task2, task3]
} else {
var task1 = mental_tasks.at(1)
var task2 = mental_tasks.at(3)
var task3 = mental_tasks.at(5)
return [task1, task2, task3]
}
}
function mentalDescription_Randomizer() {
var time = new Date()
const date = time.getDate()
if (date%2 == 0) {
var task1 = mental_descriptions.at(0)
var task2 = mental_descriptions.at(2)
var task3 = mental_descriptions.at(4)
return [task1, task2, task3]
} else {
var task1 = mental_descriptions.at(1)
var task2 = mental_descriptions.at(3)
var task3 = mental_descriptions.at(4)
return [task1, task2, task3]
}
}
const server = app.listen(port, () => {
console.log('App is running on port %port%.'.replace('%port%',port))
})
app.post('/app/login', (req, res) => {
const { username, password, time } = req.body;
const insert = accesslogdb.prepare('INSERT INTO accesslog (username, password, time) VALUES (?, ?, ?)');
const run = insert.run(username, password, time);
res.status(200);
res.json({
message : "user loggedin successfully"
});
})
app.post('/app/signup', (req, res) => {
const { username, password, time } = req.body;
const insert = userdb.prepare('INSERT INTO userlist (username, password, time) VALUES (?, ?, ?)');
const run = insert.run(username, password, time);
res.status(200);
res.json({
message : "user signed up successfully"
});
})
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html')
})
app.get('/login', (req, res) => {
res.sendFile(__dirname + '/public/login.html')
})
app.get('/signup', (req, res) => {
res.sendFile(__dirname + '/public/signup.html')
})
app.get('/mentaltasks', (req, res) => {
res.sendFile(__dirname + '/public/mentaltasks.html')
})
app.get('/physicaltasks', (req, res,) => {
res.sendFile(__dirname + '/public/physicaltasks.html')
})
app.get('/app',(req, res) => {
res.status(200).end("Welcome! Let's complete some tasks!\nGo to physical for your physical tasks\nGo to mental for your mental tasks")
})
app.get('/app/physical', (req, res, next) => {
var msg = 'Physical wellbeing is vital to the longevity and comfort of the body. These are you physical tasks for the day: '
var tasks = date_Randomizer()
res.status(200).json({tasks})
})
app.get('/app/physical/task1', (req, res, next) => {
var tasks = physicalDescription_Randomizer()
res.status(200).json({tasks})
})
app.get('/app/physical/task2', (req, res, nextt) => {
var tasks = physicalDescription_Randomizer()
res.status(200).json({tasks})
})
app.get('/app/physical/task3', (req, res, next) => {
var tasks = physicalDescription_Randomizer()
res.status(200).json({tasks})
})
app.get('/app/mental', (req, res, next) => {
var msg = 'Mental wellbeing is vital to the longevity and comfort of the body. These are you mental tasks for the day: '
var tasks = randomiz_mental()
res.status(200).json({tasks})
})
app.get('/app/mental/task1', (req, res, next) => {
var tasks = mentalDescription_Randomizer()
res.status(200).json({tasks})
})
app.get('/app/mental/task2', (req, res, next) => {
var tasks = mentalDescription_Randomizer()
res.status(200).json({tasks})
})
app.get('/app/mental/task3', (req, res, next) => {
var tasks = mentalDescription_Randomizer()
res.status(200).json({tasks})
})
app.use(function(req, res) {
res.status(404).send("Endpoint does not exist")
})