-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
105 lines (92 loc) · 2.94 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
const express = require('express');
const app = express();
const MongoClient = require('mongodb').MongoClient;
const cors = require('cors');
const PORT = 8000;
require('dotenv').config()
app.set('view engine', 'ejs');
app.use(express.json());
app.use(cors());
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }))
let db,
dbConnectionStr = process.env.DB_STRING,
dbName = 'exercise'
// Connect to database
MongoClient.connect(dbConnectionStr, { useUnifiedTopology: true })
.then(client => {
console.log(`Connected to ${dbName} Database`);
db = client.db(dbName);
})
.catch(err => console.error(err));
// Randomly select an exercise from the three categories
function randomEx(arr) {
const index = Math.floor(Math.random() * arr.length);
const item = arr[index];
return item;
}
// Sort exercises from db into categories based on type of exercise
function routine(arr) {
const upper = arr.filter(exercise => exercise.type === 'Upper body');
const core = arr.filter(exercise => exercise.type === 'Core');
const lower = arr.filter(exercise => exercise.type === 'Lower body');
return [randomEx(upper), randomEx(core), randomEx(lower)]
}
app.get('/', (req, res) => {
db.collection('library').find().toArray()
.then(data => {
const sample = routine(data);
res.render('index.ejs', { info: sample });
})
.catch(err => console.error(err))
});
app.get('/api/library', (req, res) => {
db.collection('library').find().toArray()
.then(data => {
res.json(data)
})
.catch(err => console.error(err))
});
app.get('/api/library/:title', (req, res) => {
const id = req.params.title;
db.collection('library').findOne({ title: id })
.then(data => {
if (data) {
res.json(data);
} else {
res.status(404).end();
}
})
.catch(err => console.error(err))
});
app.delete('/api/library/:title', (req, res) => {
const id = req.params.title;
db.collection('library').deleteOne({ title: id })
.then(result => {
console.log('Exercise deleted');
res.json('Exercise deleted');
})
.catch(err => console.error(err))
});
app.post('/api/library', (req, res) => {
if (!req.body.title) {
return res.status(400).json({
error: 'content missing'
});
}
db.collection('library').insertOne({
title: req.body.title,
instructions: req.body.instructions,
type: req.body.type,
link: req.body.type
})
.then(result => {
console.log('Exercise added');
res.json('Exercise added')
})
.catch(err => console.error(err))
});
// Make connection with host (local or cloud)
app.listen(process.env.PORT || PORT, () => {
console.log(`Server running on port ${PORT}`);
});