-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (59 loc) · 2.41 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 http = require("http");
const url = require("url");
const fs = require("fs").promises;
const products = require('./data/data.json');
const server = http.createServer(async (req, res) => {
console.log(req.headers);
//const myUrl = new URL(req.url, `http://${req.headers.host}/`);
const myUrl = new URL(req.url, `http://localhost:4000`);
const pathname = myUrl.pathname;
const id = myUrl.searchParams.get("id");
console.log(pathname, id);
if (pathname === "/") {
const html = await fs.readFile("./view/index.html", "utf-8");
const ALLMAINPRODUCTS = await fs.readFile("./view/main/index.html", "utf-8");
let allTheProducts = '';
for (let index = 0; index <6; index++) {
allTheProducts += replaceTemplate(ALLMAINPRODUCTS, products
[index]);
}
html = html.replace(/<ALLMAINPRODUCTS>/g, allTheProducts);
res.writeHead(200, { "Content-Type": "text/html" });
res.end(html);
} else if (pathname === "/product" && id >= 0 && id <= 5) {
let html = await fs.readFile("./view/product.html", "utf-8");
const product = products.find((p) => p.id === id);
html = replaceTemplate(html, product);
res.writeHead(200, { "Content-Type": "text/html" });
res.end(html);
} else if (/\.(png)$/i.test(req.url)) {
const image = await fs.readFile(`./public/images/${req.url.slice(1)}`);
res.writeHead(200, { "Content-Type": "image/png" });
res.end(image);
} else if (/\.(css)$/i.test(req.url)) {
const css = await fs.readFile(`./public/css/index.css`);
res.writeHead(200, { "Content-Type": "text/css" });
res.end(css);
} else {
res.writeHead(404, { "Content-Type": "text/html" });
res.end("<h1>File Not Found</h1>");
}
});
server.listen(4000);
function replaceTemplate(html, product) {
html = html.replace(/<%IMAGE%>/g, product.image);
html = html.replace(/<%NAME%>/g, product.name);
let price = product.originalPrice;
if(product.hasDiscount){
price = (price *(100 - product.hasDiscount));
}
html = html.replace(/<%NEWPRICE%>/g, `$${price}.00`);
html = html.replace(/<%OLDPRICE%>/g, `$${product.originalPrice}`);
html = html.replace(/<%ID%>/g, product.id);
if(product.hasDiscount){
html = html.replace(/<%DISCOUNTRATE%>/g, `<span>${product.discount}% Off</span>`);
} else {
html = html.replace(/<%DISCOUNTRATE%>/g, ``);
}
return html;
}