-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.js
177 lines (157 loc) · 5.92 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const http = require('http');
const url = require('url');
const path = require('path');
const fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
// Path to the blogs file
const blogsFilePath = path.join(__dirname, 'Backend/blogs.txt');
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
if (req.method === 'GET' && pathname === '/') {
fs.readFile(blogsFilePath, 'utf8', (err, data) => {
if (err) {
console.log(err);
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Internal Server Error');
return;
}
const indexStyleFilePath = path.join(__dirname, 'static/index_css.txt');
let addstyles = "";
addstyles = fs.readFileSync(indexStyleFilePath).toString();
const blogs = JSON.parse(data);
let html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>theNewtonCode Blogs</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<style>
${addstyles}
</style>
<header>
<h1>Welcome to My Personal Blog</h1>
<nav>
<a href="/">Home</a>
<a href="/about.html">About</a>
<a href="/contact.html">Contact</a>
</nav>
</header>
<main>
<section class="blog-posts">
`;
for (const blog in blogs) {
html += `
<article>
<h2><a href="/${blog}">${blogs[blog].title}</a></h2>
<p>Published on ${blogs[blog].time}</p>
<p>${blogs[blog].content.substring(0, 100)}... <a href="/${blog}">Read more</a></p>
</article>
`;
}
html += `
</section>
</main>
<footer>
<p>© 2024 Personal Blog | theNewtonCode. All rights reserved.</p>
</footer>
</body>
</html>
`;
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
});
// Serve individual blog posts
} else if (req.method === 'GET' && pathname !== '/' && pathname !== '/about.html' && pathname !== '/contact.html') {
const blogName = pathname.slice(1);
fs.readFile(blogsFilePath, 'utf8', (err, data) => {
if (err) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Internal Server Error');
return;
}
const StyleFilePath = path.join(__dirname, 'static/post_css.txt');
let addstyles = "";
addstyles = fs.readFileSync(StyleFilePath).toString();
const blogs = JSON.parse(data);
const blog = blogs[blogName];
if (blog) {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${blog.title}</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<style>
${addstyles}</style>
<header>
<h1>${blog.title}</h1>
<nav>
<a href="/">Home</a>
<a href="/about.html">About</a>
<a href="/contact.html">Contact</a>
</nav>
</header>
<main>
<article>
<p>Published on ${blog.time}</p>
<p>${blog.content}</p>
<p><a href="/">Back to Home</a></p>
</article>
</main>
<footer>
<p>© 2024 Personal Blog | theNewtonCode. All rights reserved.</p>
</footer>
</body>
</html>
`;
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>Blog Post Not Found</h1>');
}
});
}
else if(req.method === 'GET' && pathname === '/about.html'){
const aboutFile = path.join(__dirname, 'static/about.html');
fs.readFile(aboutFile, (err, data)=>{
if(err){
res.writeHead(500), {'Content-Type':'text/plain'}
res.end('Some internal server error');
return;
}
res.writeHead(200, {'Content-Type':'text/html'});
res.end(data);
})
}
else if(req.method === 'GET' && pathname === '/contact.html'){
const aboutFile = path.join(__dirname, 'static/contact.html');
fs.readFile(aboutFile, (err, data)=>{
if(err){
res.writeHead(500), {'Content-Type':'text/plain'}
res.end('Some internal server error');
return;
}
res.writeHead(200, {'Content-Type':'text/html'});
res.end(data);
})
}
else{
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('Not Found');
}
});
//serving
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});