-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (66 loc) · 1.96 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
69
70
71
const { readFileSync } = require("fs");
const http = require("http");
console.log("Reading Data... 🔃");
const data = JSON.parse(
readFileSync(`${__dirname}/dev-data/data.json`, "utf-8")
);
console.log("Finished reading Data ☑️");
console.log("Reading Templates... 🔃");
let overview = readFileSync(`${__dirname}/templates/overview.html`, "utf-8");
let cardTemplate = readFileSync(
`${__dirname}/templates/template-product-card.html`,
"utf-8"
);
let product = readFileSync(`${__dirname}/templates/product.html`, "utf-8");
console.log("Finished reading Template ☑️");
console.log("Modifying Templates... 🚧");
console.log("Generating Product Cards... ⚒️");
const cardHTML = data.map((product) => {
let modifyableTemplate = cardTemplate;
modifyableTemplate = modifyableTemplate.replace(
/{%PRODUCT_IMAGE%}/g,
product.image
);
modifyableTemplate = modifyableTemplate.replace(
/{%PRODUCT_NAME%}/g,
product.productName
);
modifyableTemplate = modifyableTemplate.replace(
/{%IS_ORGANIC%}/g,
product.organic ? "" : "not-organic"
);
modifyableTemplate = modifyableTemplate.replace(
/{%QUANTITY%}/g,
product.quantity
);
modifyableTemplate = modifyableTemplate.replace(/{%PRICE%}/g, product.price);
modifyableTemplate = modifyableTemplate.replace(
/{%PRODUCT_ID%}/g,
product.id
);
return modifyableTemplate;
});
let cards = "";
cardHTML.forEach((element) => {
cards += element;
});
console.log("Filling cards into Overview... ➕➕");
overview = overview.replace("{%PRODUCT_CARDS%}", cards);
const server = http.createServer((req, res) => {
const url = req.url;
if (url === "/api/products") {
res.writeHead(200, {
"Content-type": "application/json",
});
res.end(JSON.stringify(data));
}
if (url === "/overview") {
res.writeHead(200, {
"Content-type": "text/html",
});
res.end(overview);
}
});
server.listen(8000, () => {
console.log("server started at port 8000");
});