-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
130 lines (109 loc) · 3.1 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
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const url = require('url')
const queryString = require('querystring')
const sqlite3 = require('sqlite3').verbose()
let db = new sqlite3.Database('./db.db')
let lastID = 4
app.get("/get_cocktails", (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.sendFile(__dirname + "/new_cocktails.json")
})
app.get("/dataStatic", (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.sendFile(__dirname + "/db.json")
})
app.get("/dataSQLite", (req, res) => {
let sql = 'SELECT * FROM studentDB'
let resObj = {
res: {},
status: false
}
db.all(sql, [], (er, rows) => {
if (er) {
console.log(er)
} else {
resObj.status = true
resObj.res = rows
}
res.setHeader('Access-Control-Allow-Origin', '*')
res.json(resObj)
})
})
app.get("/edit", (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
let parsedUrl = url.parse(req.url)
let parsedQS = queryString.parse(parsedUrl.query)
let sql = 'UPDATE studentDB SET studentName=(?), surname=(?), fatherName=(?), birthDate=(?), severalMark=(?) WHERE id=(?)'
let resObj = {
res: "FAILED",
success: false
}
db.run(sql, [
parsedQS.studentName,
parsedQS.surname,
parsedQS.fatherName,
parsedQS.birthDate,
parsedQS.severalMark,
parsedQS.studentID
], (er) => {
if (er) {
console.log(er)
} else {
resObj.res = "SUCCESS"
resObj.success = true
}
console.log("HERE!!!")
res.json(resObj)
})
})
app.get("/delete", (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
let parsedUrl = url.parse(req.url)
let parsedQS = queryString.parse(parsedUrl.query)
let sql = 'DELETE FROM studentDB WHERE id=(?)'
let resObj = {
res: false,
status: "FAILED"
}
db.run(sql, [parsedQS.id], (er) => {
if (er) {
console.log(er)
} else {
resObj.res = true
resObj.status = "SUCCESS"
}
res.json(resObj)
})
})
app.get("/add", (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
let parsedUrl = url.parse(req.url)
let parsedQS = queryString.parse(parsedUrl.query)
console.log(parsedQS)
let sql = 'INSERT INTO studentDB ("studentName", "surname", "fatherName", "birthDate", "severalMark", "id") VALUES (?, ?, ?, ?, ?, ?)'
let resObj = {
res: false,
status: 'FAILED'
}
db.run(sql, [
parsedQS.studentName,
parsedQS.surname,
parsedQS.fatherName,
parsedQS.birthDate,
parsedQS.severalMark,
parsedQS.id
], (er) => {
if (er) {
console.log(er)
} else {
resObj.res = true
resObj.status = 'SUCCESS'
}
res.json(resObj)
})
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})