-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (95 loc) · 2.99 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
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
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
import session from "express-session";
import passport from "passport";
import GoogleStrategy from "passport-google-oauth";
const app = express();
const PORT = 3000;
const API_URL = "http://localhost:5000";
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(session({
secret: "THESECRETKEY",
resave: false,
saveUninitialized: true
}))
app.use(passport.initialize());
app.use(passport.session());
// Show all records from books API
app.get("/", async (req, res) => {
const response = await axios.get(`${API_URL}/books`);
try {
res.render("index.ejs", {
mainTitle: "Book review blog",
bookInfo: response.data
});
console.log("log: Fetching all records");
} catch (error) {
console.error(error);
console.log("error: Unable to fetch all records")
}
});
// Sort records using books API
app.get("/sort?", async (req, res) => {
const sortBy = req.query.by;
const response = await axios.get(`${API_URL}/books/sort?by=${sortBy}`);
try {
res.render("index.ejs", {
mainTitle: "Book review blog",
bookInfo: response.data
});
console.log("log: Sorting all records");
} catch (error) {
console.error(error);
console.log("error: Unable to sort all records");
}
});
// Add new record to books API
app.get("/new", async (req, res) => {
res.render("new.ejs", { mainTitle: "New Book review" });
});
app.post("/new", async (req, res) => {
try {
const response = await axios.post(`${API_URL}/books/new`, req.body);
res.redirect("/");
console.log(req.body);
console.log(response.data);
console.log(`log: New record added at ${response.data.id}`);
} catch (error) {
console.error(error);
console.log("error: Unable to add new record");
}
});
// Update exsting records in books API
app.get("/review/:id", async (req, res) => {
const response = await axios.get(`${API_URL}/books/${req.params.id}`);
res.render("review.ejs", {
mainTitle: "Book review",
book: response.data[0],
});
});
app.post("/update/:id", async (req, res) => {
console.log(req.body);
let response = await axios.patch(`${API_URL}/books/${req.params.id}`, req.body);
response = response.data;
try {
res.render("review.ejs", {
mainTitle: "Book review",
book: response
});
} catch (error) {
console.error(error);
console.log("error: Unable to update record");
}
});
// Delete existing record in book API
app.get("/delete/:id", async (req, res) => {
const response = await axios.delete(`${API_URL}/books/${req.params.id}`);
console.log(response.data);
res.redirect("/");
})
app.listen(`${PORT}`, (req, res) => {
console.log(`Server running on https://localhost:${PORT}`);
})