forked from cs4241-19a/a3-persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
185 lines (158 loc) · 5.58 KB
/
server.js
File metadata and controls
185 lines (158 loc) · 5.58 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
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
const http = require( 'http' ),
fs = require( 'fs' ),
mime = require( 'mime' ),
express = require('express'),
bodyParser = require('body-parser'),
low = require('lowdb'),
FileSync = require('lowdb/adapters/FileSync'),
shortid = require('shortid'),
session = require( 'express-session' ),
passport = require( 'passport' ),
Local = require( 'passport-local' ).Strategy,
responseTime = require('response-time'),
morgan = require('morgan'),
dir = 'public/',
port = 3000,
app = express(),
adapter = new FileSync('db.json'), //Make the database file
db = low( adapter )
db.defaults({ data:[] }).write()
app.use( bodyParser.json() )
app.use(express.static('public'))
app.use(responseTime()) // Adds "x-response-time" to response header
app.use(morgan("tiny"));
const myLocalStrategy = function( username, password, done ) {
const user = db.get('data').find({ username:username}).write()
if( user === undefined ) {return done( null, false, { message:'user not found' })}
else if( user.password === password ) {return done( null, { username, password })}
else{return done( null, false, { message: 'incorrect password' })}
}
passport.use( new Local( myLocalStrategy ) )
app.use( passport.initialize())
app.use( session({ secret:'cats cats cats', resave:false, saveUninitialized:false }) )
app.use( passport.session() )
passport.serializeUser( ( user, done ) => done( null, user.username ) )
passport.deserializeUser( ( username, done ) => {
const user = db.get('data').find({ username:username}).write()
console.log( 'deserializing:', name )
if( user !== undefined ) {done( null, user )}
else{done( null, false, { message:'user not found; session not restored' })}
})
const checkData = function (error, json ){
const volt = Number(json.voltage),
curr = Number(json.current),
[month, day, year] = json.date.split("/"),
[hours, minutes] = json.time.split(":")
if (volt <=0){
console.log("Invalid Voltage value.")
error += "Invalid Voltage value. "
}
if (curr <=0){
console.log("Invalid Current value.")
error += "Invalid Current value. "
}
if (month <1 || month >12){
console.log("Invalid Month.")
error += "Invalid Month. "
}
if (day <1 || day >31){
console.log("Invalid Day.")
error += "Invalid Day. "
}
if (year <0 || year >99){
console.log("Invalid Year.")
error += "Invalid Year. "
}
if (hours <0 || hours >24){
console.log("Invalid Hour")
error += "Invalid Hour. "
}
if ( minutes <0 || minutes >59){
console.log("Invalid Minute.")
error += "Invalid Minute. "
}
return error
}
app.get('/', function(request, response) {response.sendFile(__dirname + '/views/index.html')})
app.post('/login', passport.authenticate( 'local'), function( req, res ) {
res.json({ status:true })
})
app.post('/addData', function( request, response){
console.log(request.body)
const json = request.body
let error = ""
error += checkData(error, json)
if (error !== "" ){
error += "Data will not be recorded"
response.writeHead(400, 'OK', {'Content-Type': 'text/plain' }) //Write a reponse back to client
response.write(error)
response.end()
}
else{
let userExists = false
for (let entry of db.get('data').write()){
if (entry.username === json.username){userExists = true}
}
json.power = json.voltage * json.current * json.current
json.id = (shortid.generate())
db.get( 'data' ).push(json).write()
let message = "Data recorded. "
if (userExists === false){
message += "New user created."
}
response.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) //Write a reponse back to client
response.write(message)
response.end()
}
})
app.post("/delData", function( request, response){
const json = request.body
console.log(json)
db.get('data').remove({id:json.id.trim()}).write()
response.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) //Write a reponse back to client
response.write("If the record was found, it was deleted")
response.end()
})
app.post("/modData", function( request, response){
const json = request.body
let error = ""
error += checkData(error, json)
if (error !== "" ){
error += "Data will not be modified"
response.writeHead(400, 'OK', {'Content-Type': 'text/plain' }) //Write a reponse back to client
response.write(error)
response.end()
}
else{
json.power = json.voltage * json.current * json.current
db.get('data').find({ id:json.id.trim()}).assign(json).write()
response.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) //Write a reponse back to client
response.write("Data has been recorded")
response.end()
}
})
app.post("/getData", function(request, response){
const allData = db.get( 'data' ).value(),
username = request.body.username,
password = request.body.password
let userData = []
if (username === 'admin' && password === 'admin'){
userData = allData
}
else{
for (let entry of allData){
if (entry.username === request.body.username
&& entry.password === request.body.password) {
userData.push(entry)
}
}
}
console.log(userData)
const dataString = JSON.stringify(userData)
response.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) //Write a reponse back to client
response.write(dataString)
response.end()
})
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port)
})