-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (39 loc) · 1.61 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
const fs = require('fs');
const http = require('http');
const url = require('url');
const replaceTemplate = require('./modules/replaceTemplate');
const tempOverview = fs.readFileSync(`${__dirname}/templates/template-overview.html`,'utf-8');
const tempCard= fs.readFileSync(`${__dirname}/templates/template-card.html`,'utf-8');
const tempProduct = fs.readFileSync(`${__dirname}/templates/template-product.html`,'utf-8');
const objectData = fs.readFileSync(`${__dirname}/dev-data/data.json`,'utf-8');
const productData = JSON.parse(objectData);
//////////////////SERVER//////////
const server = http.createServer((req,resp)=>{
const {query,pathname} =url.parse(req.url,true);
//Overview Page
if(pathname==='/' || pathname==='/overview'){
resp.writeHead(200,{'Content-Type':'text/html'});
const cardsHtml = productData.map(el=> replaceTemplate(tempCard,el)).join('');
const outPut = tempOverview.replace(/{%PRODUCT_CARDS%}/g,cardsHtml);
resp.end(outPut);
//product Page
}else if(pathname==='/product'){
resp.writeHead(200,{'Content-Type':'text/html'});
const product = productData[query.id];
const outPut = replaceTemplate(tempProduct,product);
resp.end(outPut);
//api
}else if(pathname==='/api'){
resp.writeHead(200,{'Content-Type':'application/json'})
//Not Found
}else{
resp.writeHead(400,{
'Content-Type':'text/html',
'my-own-header':'hello-word'
});
resp.end('<h3>Page not found</h3>')
}
});
server.listen(5000,'127.0.0.1',()=>{
console.log('listing port 5000');
});