-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
79 lines (67 loc) · 1.67 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
var express = require("express");
var cors = require("cors");
var rateLimit = require("express-rate-limit");
var app = express();
const {
getHymnByNumber,
getCategories,
getHymnsInCategory,
searchHymn
} = require("./utils/APIFunctions");
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
message: {
success: false,
message:
"Too many requests! Please try again in the next 15-20 mins or contact @marvinjudehk on twitter if this perists longer"
}
});
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(limiter);
app.get("/api/v1/hymn/:id", function(req, res) {
const hymn = getHymnByNumber(req.params.id);
if (!hymn)
res.status(404).send({
success: false,
message: "couln't find anything"
});
res.status(200).send({
success: true,
data: hymn
});
});
app.get("/downloads", (req, res) => {
res.sendFile(`${__dirname}/views/download.html`);
});
app.get("/download", (req, res) => {
res.download(`${__dirname}/content/db.json`, "hymns.json");
});
app.get("/api/v1/category", function(req, res) {
res.status(200).send({
success: true,
data: getCategories()
});
});
app.get("/api/v1/category/:category", function(req, res) {
const list = getHymnsInCategory(req.params.category);
if (!list)
res.status(404).json({
success: false,
message: "couln't find anything"
});
return res.status(404).send({
success: true,
data: list
});
});
app.get("/api/v1/search/:term", function(req, res) {
const matches = searchHymn(req.params.term);
res.status(200).json({
success: true,
data: matches
});
});
module.exports = app;