-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (58 loc) · 1.92 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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const cors = require("cors");
// Express installation
const app = express();
const port = 8000;
app.use(express.static("views"));
app.use(cors());
app.get("/trypost", (req, res) => {
res.sendFile(__dirname + "/views/trypost.html");
});
// Connect to MongoDB
mongoose
.connect("mongodb+srv://rakarmp12:rakarmp12@cluster0.htim4l5.mongodb.net/", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("MongoDB Connected"))
.catch((err) => console.log("Error to connect MongoDB", err));
// Middleware untuk parsing body request
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Import controller
const quoteController = require("./controllers/quoteController");
// Home route
app.get("/", quoteController.home);
// Mendapatkan quote acak
app.get("/quotes/random", quoteController.getRandomQuote);
// Mendapatkan quote acak berdasarkan judul anime
app.get(
"/quotes/random/:animeTitle",
quoteController.getRandomQuoteByAnimeTitle
);
// Mendapatkan quote acak berdasarkan karakter anime
app.get(
"/quotes/random/character/:animeCharacter",
quoteController.getRandomQuoteByAnimeCharacter
);
// Mendapatkan 5 quote acak
app.get("/quotes/random/5", quoteController.getFiveRandomQuotes);
// Mendapatkan 5 quote acak berdasarkan judul anime
app.get(
"/quotes/random/5/:animeTitle",
quoteController.getFiveRandomQuotesByAnimeTitle
);
// Mendapatkan 5 quote berdasarkan karakter anime
app.get(
"/quotes/character/:animeCharacter",
quoteController.getFiveQuotesByAnimeCharacter
);
// Mendapatkan quote dengan pagination
app.get("/quotes", quoteController.getQuotesWithPagination);
app.post("/quotes", quoteController.createQuote);
// Menjalankan server pada port 3000
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});