-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (54 loc) · 1.4 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
const express = require("express");
const path = require("path");
const app = express();
const dataPemenang = require("./pemenang.json");
app.set("view engine", "ejs");
// middleware:
app.use(express.static("public"));
app.use(express.json());
// Buat fungsi loggin, yang dipanggil di setiap request:
const createLog = (req, res, next) => {
res.on("finish", function () {
console.log(
req.method,
decodeURI(req.url),
res.statusCode,
res.statusMessage
);
});
next();
};
app.use(createLog);
// Import userRouter:
const userRouter = require("./userRouter");
app.use(userRouter);
// Bikin website sederhana
app.get("/", (req, res) => {
res.render("index");
});
app.get("/about", (req, res) => {
res.render("about");
});
app.get("/projects", (req, res) => {
res.render("projects");
});
app.get("/belajar-ejs", (req, res) => {
res.render("belajar-ejs.ejs", { jsonString: JSON.stringify(dataPemenang) });
});
// 1. Unmatched router handler:
app.use((req, res, next) => {
res.status(404).json({
status: "gagal mengakses halaman",
message: "halaman tersebut tidak ada!!!",
});
});
// 2. Error handler:
app.use((err, req, res, next) => {
// ngapain disini terserah kita:
res.sendFile(path.join(__dirname, "/views/error.html"));
});
// Nyalain servernya:
app.listen(9000, () => {
console.log("halo, app sudah nyalaaaa~~~");
console.log("selamat datang~~~!!!");
});