-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (50 loc) · 2.24 KB
/
Copy pathserver.js
File metadata and controls
53 lines (50 loc) · 2.24 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
const http = require('http');
const fs = require('fs');
const url = require('url');
const replaceHtml = require('./module');
let html;
try {
html = fs.readFileSync('./index.html', 'utf-8');
} catch (err) {
console.error("Error reading index.html:", err);
}
let products = JSON.parse(fs.readFileSync('./products.json', 'utf-8'));
let productListItem = fs.readFileSync('./productList.html', 'utf-8');
let productDetailItem = fs.readFileSync('./product-details.html', 'utf-8');
let contacts=fs.readFileSync('./contacts.html','utf-8')
const server = http.createServer((request, response) => {
let { query, pathname: path } = url.parse(request.url, true);
console.log(`Received request for: ${path}`);
if (path === '/home' || path === '/') {
response.writeHead(200, { 'content-type': 'text/html' });
response.end(html.replace('{{%CONTENT%}}', 'You are on the home page'));
} else if(path === '/contactus'){
response.writeHead(200, { 'content-type': 'text/html' });
response.end(html.replace('{{%CONTENT%}}', contacts));
} else if (path === '/products') {
if (!query.id) {
let productHtmlArray = products.map((prod) => {
return replaceHtml(productListItem, prod);
});
response.writeHead(200, { 'content-type': 'text/html' });
let productResponse = html.replace('{{%CONTENT%}}', productHtmlArray.join(''));
response.end(productResponse);
} else {
let prod = products.find(p => p.id === Number(query.id));
if (prod) {
let productDetailResponseHtml = replaceHtml(productDetailItem, prod);
response.writeHead(200, { 'content-type': 'text/html' });
response.end(html.replace('{{%CONTENT%}}', productDetailResponseHtml));
} else {
response.writeHead(404, { 'content-type': 'text/html' });
response.end(html.replace('{{%CONTENT%}}', 'Product not found'));
}
}
} else {
response.writeHead(404, { 'content-type': 'text/html' });
response.end(html.replace('{{%CONTENT%}}', 'Page not found'));
}
});
server.listen(5500, '127.0.0.1', () => {
console.log("Server has started on port 5500");
});