-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
109 lines (93 loc) · 2.78 KB
/
App.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
require('dotenv').config();
const express = require('express');
const database = require("./data/Data");
const { v4: uuid } = require('uuid');
const app = express();
// const flash = require("express-flash")
// const { addRequest, createBin } = require('./data/configs/Data');
app.set('view engine', 'pug')
app.use(express.static('static'))
// app.use(flash());
const PORT = 3003;
const hostname = process.env.HOST;
app.get('/', (req, res) => {
res.status(200).render('home', { hostname });
});
app.get('/bin/:bin_id/view', async (req, res) => {
let bin_id = req.params.bin_id;
const binExists = await database.binExists(bin_id);
if (binExists) {
const reqs = await database.getRequests(bin_id);
res.render('bin', {
binid: bin_id,
reqs,
hostname
});
} else {
res.redirect('/');
}
});
//adds a request to a bin if the bin exists
app.get('/bin/:bin_id', express.raw({ inflate: true, limit: '50mb', type: () => true }), async (req, res) => { //
let bin_id = req.params.bin_id;
const binExists = await database.binExists(bin_id);
if (binExists) {
await database.addRequest(bin_id, req) // we may need to add a error handling || if the db update does not succeed
res.sendStatus(201);
} else {
res.sendStatus(404);
}
});
//adding post requests to a bin
app.post('/bin/:bin_id/', express.raw({ inflate: true, limit: '50mb', type: () => true }), async (req, res) => {
let bin_id = req.params.bin_id;
const binExists = await database.binExists(bin_id);
if (binExists) {
await database.addRequest(bin_id, req)
res.sendStatus(201);
} else {
res.sendStatus(404);
}
})
//create a new bin and return its newbinID as json
app.post('/', async (req, res) => {
let newBin = await database.createBin(req)
res.json({ newBin })
});
app.post('/verifybins', express.json(), async (req, res) => {
let clientBins = req.body;
let promiseArr = clientBins.map(bin_id => {
return new Promise( (resolve) => {
database.binExists(bin_id).then( val => {
if(val) {
resolve(bin_id)
} else {
resolve("")
}
})
})
})
let existingBins = await Promise.all(promiseArr)
existingBins = existingBins.filter( val => val !== "");
res.json(existingBins);
})
//delete a bin
app.post('/:bin_id/destroy', async(req, res) => {
let bin_id = req.params.bin_id;
console.log(bin_id);
const binExists = await database.binExists(bin_id);
if (binExists) {
await database.deleteBin(bin_id);
// req.flash("Success", `Bin ${bin_id} has been deleted`);
res.redirect('/');
} else {
res.sendStatus(404);
}
})
app.get('*', async (req, res) => {
res.redirect('/');
});
app.post('*', async (req, res) => {
res.redirect('/')
});
app.listen(PORT, () => console.log("we are listening on port" + PORT))