-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (34 loc) · 1023 Bytes
/
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
// import express from "express";
const express = require("express");
// import data1 from "./singers.json" assert {type: "json"};
// const { singers } = data1;
const { singers } = require("./singers.json");
const app = express();
app.get("/", (req, res) => {
res.send("網站首頁");
});
// /singer/:id.html
app.get("/singer/:id.html", (req, res) => {
const { id } = req.params;
let result = singers.find((singer) => singer.id === parseInt(id));
if (!result) {
res.status(404).send("<h1>找不到歌手</h1>");
return false;
}
res.send(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${result.singer_name} 的頁面</title>
</head>
<body>
<h1>${result.singer_name} 的頁面</h1>
<img src="${result.singer_img}">
</body>
</html>`);
// res.json(result);
});
app.listen(3000, () => {
console.log("running at http://localhost:3000");
});