-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
80 lines (69 loc) · 2.02 KB
/
index.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
const express = require("express");
const data = require("./book-of-mormon.json");
const app = express();
const PORT = process.env.PORT || 8000;
const randomIndex = (arr) => Math.floor(Math.random() * arr.length);
const books = [
"1nephi",
"2nephi",
"jacob",
"enos",
"jarom",
"omni",
"wordsofmormon",
"mosiah",
"alma",
"helaman",
"3nephi",
"4nephi",
"mormon",
"ether",
"moroni",
];
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.get("/", (req, res) =>
res.json({
message: "Welcome to the ultimate Book of Mormon API! Instructions are here: https://github.com/BraydenTW/book-of-mormon-api/",
})
);
app.get("/random", (req, res) => {
const bookIndex = randomIndex(data.books);
const chapterIndex = randomIndex(data.books[bookIndex].chapters);
const verseIndex = randomIndex(
data.books[bookIndex].chapters[chapterIndex].verses
);
res.json(data.books[bookIndex].chapters[chapterIndex].verses[verseIndex]);
});
app.get("/random/:book", (req, res) => {
const bookIndex = books.indexOf(req.params.book);
const chapterIndex = randomIndex(data.books[bookIndex].chapters);
const verseIndex = randomIndex(
data.books[bookIndex].chapters[chapterIndex].verses
);
res.json(data.books[bookIndex].chapters[chapterIndex].verses[verseIndex]);
});
app.get("/random/:book/:chapter", (req, res) => {
const bookIndex = books.indexOf(req.params.book);
const chapterIndex = parseInt(req.params.chapter) - 1;
const verseIndex = randomIndex(
data.books[bookIndex].chapters[chapterIndex].verses
);
res.json(data.books[bookIndex].chapters[chapterIndex].verses[verseIndex]);
});
app.get("/:book/:chapter/:verse", (req, res) => {
res.json(
data.books[books.indexOf(req.params.book)].chapters[
parseInt(req.params.chapter) - 1
].verses[parseInt(req.params.verse) - 1]
);
});
app.listen(PORT, () => {
console.log("hii");
});