-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
96 lines (76 loc) · 2.42 KB
/
Copy pathserver.js
File metadata and controls
96 lines (76 loc) · 2.42 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
require('dotenv').config();
const express = require('express')
const app = express()
const PORT = process.env.PORT || 4999;
//middleware
app.use(express.json())
app.use(express.static('public'))
app.use(require('cors')())
app.use(mw)
function mw(req, res, next) {
console.log('Hit the MIDDLEWARE')
const { id } = req.query
if (id != 8) {
return res.sendStatus(403)
}
next()
}
//TEMP DATABASE
const db = []
//SCHEDULER
function cron(ms, fn) {
async function cb() {
clearTimeout(timeout)
await fn()
timeout = setTimeout(cb, ms)
}
let timeout = setTimeout(cb, ms)
return () => { }
}
function consoleDB() {
console.log('DB= ', db)
}
cron(1000, consoleDB)
//This gets overrun by the html file when
// there is app.use(express.static('public'))
app.get('/', (req, res) => {
console.log("You have reached the home route: GET ")
res.status(200).send({ "yourMessage": "GET: Opened web" })
})
app.post('/api/info', (req, res) => {
const { information } = req.body
console.log('The posted message: ', information)
db.push(information)
console.log('DB: ', db)
res.status(201).json({ "yourMessage": `POST: ${information}` })
})
app.put('/api/create', (req, res) => {
const { information } = req.body
db.push(information)
console.log('Added new item:', information)
return res.status(201).json({ "yourMessage": `PUT CREATE: ${information}` })
})
app.put('/api/update', (req, res) => {
const { information } = req.body
const { newInformation } = req.body
const index = db.findIndex(newInformation => newInformation === information)
if (index === -1) {
return res.status(404).json({ "yourMessage": "PUT UPDATE: No item to update in database" })
}
db.splice(index, 1, newInformation);
console.log('Updated new information:', newInformation)
res.status(200).json({ "yourMessage": `PUT UPDATE: "${information}" to "${newInformation}"` })
});
app.delete('/delete', (req, res) => {
if (db.length > 0) {
const info = db[0]
const deleted = info
db.splice(0, 1)
res.status(200).json({ "yourMessage": `DELETE: ${deleted}` })
console.log('The deleted message: ', info)
} else {
console.log('Empty DB LIST')
res.status(404).json({ "yourMessage": "DELETE: Database is empty" })
}
})
app.listen(PORT, () => console.log(`Server has started on port: ${PORT}`))