-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
67 lines (58 loc) · 2.15 KB
/
Copy pathapp.js
File metadata and controls
67 lines (58 loc) · 2.15 KB
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
const fs= require('fs')
const http = require('http')
const url = require('url')
const superagent = require("superagent")
var data=fs.readFileSync(`${__dirname}/data.json`,'utf-8');
var jsondata=JSON.parse(data);
var card=fs.readFileSync(`${__dirname}/card.html`,'utf-8');
var oview=fs.readFileSync(`${__dirname}/overview.html`,'utf-8');
var product=fs.readFileSync(`${__dirname}/product.html`,'utf-8');
function replaceOver(card,element){
let output=card.replace(/{%PRODUCTNAME%}/g,element.productName);
output=output.replace(/{%IMAGE%}/g,element.image);
output=output.replace(/{%QUANTITY%}/g,element.quantity);
output=output.replace(/{%PRICE%}/g,element.price);
output=output.replace(/{%NUTRIENTS%}/g,element.nutrients);
output=output.replace(/{%DISC%}/g,element.description);
output=output.replace(/{%ID%}/g,element.id);
output=output.replace(/{%PLACE%}/g,element.from);
if(!element.organic) output=output.replace(/{%NOTORGANIC%}/g,"not-organic");
return output;
}
var server = http.createServer((req,res)=>{
const { query, pathname } = url.parse(req.url, true);
if (pathname === '/' || pathname === '/overview') {
res.writeHead(200, {
'Content-type': 'text/html'
});
const cardObj = jsondata.map(el=>replaceOver(card,el));
//console.log(cardObj).join(" ");
const final = oview.replace(/{%PRODUCT_CARDS%}/g,cardObj)
res.end(final);
// Product page
} else if (pathname === '/product') {
res.writeHead(200, {
'Content-type': 'text/html'
});
const el = jsondata[query.id];
const cardObj = replaceOver(product,el);
res.end(cardObj);
// API
} else if (pathname === '/api') {
res.writeHead(200, {
'Content-type': 'application/json'
});
// console.log(data);
res.end(data);
// Not found
} else {
res.writeHead(404, {
'Content-type': 'text/html',
'my-own-header': 'hello-world'
});
res.end('<h1>Page not found!</h1>');
}
});
server.listen(3000,()=>{
console.log("listerning ......");
})