-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
131 lines (111 loc) · 2.9 KB
/
server.js
File metadata and controls
131 lines (111 loc) · 2.9 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
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
131
/* eslint no-use-before-define: 0 */
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const jwt = require('express-jwt');
const jwksRsa = require('jwks-rsa');
const Models = require('./db');
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use(bodyParser.json());
app.use(express.static(__dirname));
const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: 'https://vinnieking06.auth0.com/.well-known/jwks.json',
}),
audience: 'https://vinnieking06/api/v2/',
issuer: 'https://vinnieking06.auth0.com/',
algorithms: ['RS256'],
});
app.get('/:user/init', checkJwt, (req, res) => {
Models.User.findOne({ where: { id: req.params.user } })
.then((user) => {
if (user) {
getNotes(req, res)
.then((notes) => {
res.json(notes);
});
} else {
createUser(req.params.user)
.then(() => {
res.json([]);
});
}
})
.catch(() => {
res.send(500);
});
});
app.post('/:user/notes', checkJwt, (req, res) => {
Models.Note.create({ userId: req.params.user, data: req.body.data, title: req.body.title })
.then((note) => {
res.json(note);
})
.catch(() => {
res.json(500);
});
});
app.get('/:user/notes/', checkJwt, (req, res) => {
getNotes(req)
.then((notes) => {
res.json(notes);
})
.catch(() => {
res.json(500);
});
});
app.get('/:user/notes/:id', checkJwt, (req, res) => {
Models.Note.findOne({ where: { id: req.params.id, userId: req.params.user } })
.then((note) => {
res.json(note);
})
.catch(() => {
res.send(500);
});
});
app.put('/:user/notes/:id', checkJwt, (req, res) => {
Models.Note.update({ data: req.body.data,
title: req.body.title }, { where: { id: req.params.id, userId: req.params.user } })
.then(() => {
Models.Note.findById(req.params.id)
.then((note) => {
res.json(note);
});
})
.catch(() => {
res.send(500);
});
});
app.delete('/:user/notes/:id', checkJwt, (req, res) => {
Models.Note.destroy({ where: { id: req.params.id, userId: req.params.user } })
.then((note) => {
res.json(note);
})
.catch(() => {
res.send(500);
});
});
app.get('*', (req, res) => {
res.sendFile(`${__dirname}/index.html`);
});
function createUser(input) {
return Models.User.create({ id: input });
}
function getNotes(req) {
const query = { order: [['updatedAt', 'DESC']], where: { userId: req.params.user, id: { $gte: 1 } } };
if (req.params.limit) {
query.limit = req.params.limit;
}
if (req.params.order) {
query.order = [['updatedAt', req.params.order]];
}
if (req.params.start) {
query.where = { id: { $gte: req.params.start } };
}
return Models.Note.findAll(query);
}
app.listen(process.env.PORT || 5000);