-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (55 loc) · 2.22 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
/// SERVER
const fs = require('fs');
const http = require('http');
const url = require('url');
const replaceTemplate = require('./modules/replaceTemplate');
const slugify = require('slugify');
const data = fs.readFileSync(`${__dirname}/dev-data/data.json`, 'utf-8');
var card = fs.readFileSync(`${__dirname}/templates/template-card.html`, 'utf-8');
var productTemp = fs.readFileSync(`${__dirname}/templates/template-product.html`, 'utf-8');
var overviewTemp = fs.readFileSync(`${__dirname}/templates/template-overview.html`, 'utf-8');
const dataObj = JSON.parse(data);
const slug = dataObj.map(el => slugify(el.productName, {lower:true}));
console.log(slug);
const server = http.createServer((req, resp) => {
const pathName = req.url;
console.log(`Node app recieved the request ${req.method} : ${pathName}`);
const {query, pathname} = url.parse(req.url, true);
if (pathname === '/' || pathname === '/overview'){
resp.writeHead(200, {
'Content-type': 'text/html'
})
const cardsHtml = dataObj.map(el => replaceTemplate(card, el)).join('');
const output = overviewTemp.replace('{%PRODUCT_CARDS%}', cardsHtml);
resp.end(output);
}
else if(pathname === '/product'){
const product = dataObj[query.id]
resp.writeHead(200, {
'Content-type': 'text/html'
})
const output = replaceTemplate(productTemp, product);
resp.end(output);
}
else if(pathname === '/api'){
resp.writeHead(200, {
'Content-type': 'application/json'
})
resp.end(data);
}
else{
resp.writeHead(404, {
'Content-type': 'text/html',
'my-own-header': 'hello-world-my-header'
});
resp.end('<h1>Page not found.</h1>')
}
// resp.end('Hello World Vishals first Node Prog here.');
});
server.listen(8000, '127.0.0.1', () => {
console.log('Node server started and listning at port 8000.')
})
// const textIn = fs.readFileSync('./txt/nodeTest.txt', 'utf-8');
// console.log(textIn);
// const textOut = `This is what we are gonna write ${textIn}`;
// fs.writeFileSync('./txt/nodeTest.txt', textOut)