-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
88 lines (64 loc) · 2.19 KB
/
server.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
76
77
78
79
80
81
82
83
84
85
86
87
88
var http = require("http")
var fs = require("fs")
var data= fs.readFileSync("data.json")
var products= fs.readFileSync("product.html")
var overview= fs.readFileSync("overview.html")
var url = require("url")
var card= fs.readFileSync("cards.html")
products+=""
card+="";
overview+=""
datajson= JSON.parse(data)
var str="" //again added comment
var replace = function(num,template){
var mytemplate;
template=template.replace(/#productName#/g,num["productName"])
template=template.replace(/#From#/g,num["from"])
template=template.replace(/#nutrients#/g,num["nutrients"])
template=template.replace(/#Quantity#/g,num["quantity"])
template=template.replace(/#Price#/g,num["price"])
template=template.replace(/#image#/g,num["image"])
template=template.replace(/#discription#/g,num["description"])
if(!num["organic"])
{template=template.replace(/#not-organic#/g,"not-organic")}
template= template.replace(/%%link%%/g,num["id"])
mytemplate=template;
return mytemplate;
}
var server = http.createServer(function(req,res)
{
res.writeHead(200,{"content-type":"json"})
var Url =url.parse(req.url,true)
var id=Url["query"]["id"];
var num = datajson[id]
for(var i =0; i<datajson.length;i++)
{
var numb = datajson[i]
// console.log(numb)
str = str + replace(numb,card)
}
if(req.url=="/"|| req.url ==""||req.url=="/homepage"|| req.url=="/HOMEPAGE")
{overview= overview.replace(/#card#/g,str)
res.write(overview)
}
else if(Url.pathname=="/product")
{
mytemplate=replace(num,products)
// mytemplate= mytemplate.replace(/%%link%%/g,num)
// console.log(mytemplate)
res.write(mytemplate)
}
else if(req.url=="/api")
{
res.write(data)
}
else
{
res.end("<h1>Error 404 page not found</h1>")
}
})
var port = process.env.PORT||3000
server.listen(port, function()
{
console.log("Server listening at 3000")
})