-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (46 loc) · 1.73 KB
/
Copy pathserver.js
File metadata and controls
61 lines (46 loc) · 1.73 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
const express = require("express");
const path = require("path");
const app = express();
const PORT = 3000;
const fs = require("fs");
const cors = require("cors");
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "views", "index.html"));
});
app.get("/home", (req, res) => {
res.sendFile(path.join(__dirname, "views", "index.html"));
});
app.get("/daftarbuku", (req, res) => {
res.sendFile(path.join(__dirname, "views", "daftarbuku.html"));
})
app.get("/api/daftarbuku", (req, res) => {
fs.readFile("books.json", "utf8", (err, data) => {
if (err) {
return res.status(500).json({ message: "Gagal membaca file JSON" });
}
let books = JSON.parse(data);
// 💡 **OLAH DATA SEBELUM DIKIRIM KE FRONTEND**
const formattedBooks = books.map((book, index) => ({
id: index + 1,
title: book.title.toUpperCase(), // Buat judul kapital semua
author: book.author || "Tidak diketahui", // Jika tidak ada author, ganti default
year: book.year,
format: book.format,
size_mb: book.size_kb ? (book.size_kb / 1024).toFixed(2) + " MB" : "N/A" // Ubah ukuran dari KB ke MB
}));
res.json(formattedBooks); // Kirim data yang sudah diolah
});
});
app.get("/kategori", (req, res) => {
res.sendFile(path.join(__dirname, "views", "kategori.html"));
})
app.get("/hubungikami", (req, res) => {
res.sendFile(path.join(__dirname, "views", "hubungikami.html"));
})
app.listen(PORT, () => {
console.log(`Server berjalan di http://localhost:${PORT}`);
});