-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
108 lines (91 loc) · 2.91 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
const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('express-jwt');
const jwks = require('jwks-rsa');
const mongoose = require('mongoose');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
app.set('json spaces', 2);
app.use(express.static('public'));
app.use('/scripts', express.static(__dirname + '/node_modules/'));
// DATABASE SETUP
mongoose.connect('mongodb://localhost:27017/webSynth');
const db = mongoose.connection;
const patchSchema = mongoose.Schema({
name: String,
ampEnv: Object,
waveType: String,
filterFreqLog: Number,
userID: String
});
const Patch = mongoose.model('Patch', patchSchema, 'patches');
db.on('error', console.error.bind(console, 'connection error:'));
// AUTHORIZATION MIDDLEWARE
const loginCheck = jwt({
secret: jwks.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: "https://interlucid.auth0.com/.well-known/jwks.json"
}),
audience: '/api',
issuer: "https://interlucid.auth0.com/",
algorithms: ['RS256']
});
// ENDPOINTS
const router = express.Router();
// TODO: add login check to post
// TODO: prevent any user from posting using another user's ID
router.route('/patches')
.get((req, res) => {
Patch.find((e, patches) => {
if(e) res.send(e);
else res.json(patches);
})
})
.post((req, res) => {
const patch = new Patch(req.body);
patch.save(e => {
if(e) res.send(e);
})
res.json(patch);
})
// TODO: add login check for put and delete
// TODO: prevent any user from putting or deleting another user's patches
router.route('/patch/:id')
.get((req, res) => {
Patch.findById(req.params.id, (e, patch) => {
if(e) res.send(e);
else res.json(patch);
});
})
.put((req, res) => {
Patch.findByIdAndUpdate(req.params.id, req.body, { new: true }, (e, patch) => {
if(e) res.send(e);
else res.json(patch);
});
})
.delete((req, res) => {
Patch.findByIdAndRemove(req.params.id, (e, patch) => {
if(e) res.send(e);
else if(patch === null) res.send('Nothing to delete');
else res.send(`Successfully deleted patch with ID ${req.params.id}`);
})
})
// get info for the current user (currently, just patches)
router.route('/user/:id')
.get((req, res) => {
Patch.find({ userID: req.params.id }, (e, patches) => {
if(e) res.send(e);
else res.json(patches);
})
})
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
})
// all of our routes will be prefixed with /api
app.use('/api', router);
const port = 3000;
app.listen(port, () => console.log(`Server listening on port ${port}!`));