Skip to content

Commit 714bd43

Browse files
committed
Update webpages
1 parent 038312f commit 714bd43

File tree

3 files changed

+114
-11
lines changed

3 files changed

+114
-11
lines changed

index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,25 @@ http
3636
// If request url is '/download'
3737
else if (req.url == "/download") {
3838
// Display links to download files
39+
40+
let html = `<ul>`
3941
fs.readdir("./uploads", function (err, files) {
4042
if (err) throw err;
4143
files.forEach(function (file) {
42-
res.write(`<a href="/uploads/${file}" download>${file}</a><br>`);
44+
html += `<li><a href="/uploads/${file}" download>${file}</a><br></li>`
4345
});
44-
res.end();
4546
});
47+
html += `</ul>`
4648

47-
res.write("<a href='/'>Back to home</a>");
49+
// Read home.html file
50+
fs.readFile("./templates/download.html", "utf8", function (err, data) {
51+
if (err) throw err;
52+
let content = data
53+
54+
content = content.replace("%CONTENT%", html)
55+
res.write(content)
56+
res.end()
57+
});
4858
} else if (req.url !== "/") {
4959
// parse URL
5060
const parsedUrl = url.parse(req.url);
@@ -90,14 +100,21 @@ http
90100
});
91101
} else {
92102
res.writeHead(200, { "Content-Type": "text/html" });
93-
res.write(
94-
'<form action="fileupload" method="post" enctype="multipart/form-data">'
95-
);
96-
res.write('<input type="file" name="filetoupload"><br>');
97-
res.write('<input type="submit">');
98-
res.write("</form>");
99-
res.write("Download files: <a href='/download'>Download</a>");
100-
return res.end();
103+
104+
// Read template.html file synchronously
105+
fs.readFile("./templates/home.html", "utf8", function (err, data) {
106+
if (err) throw err;
107+
let content = data
108+
.replace("%CONTENT%", `
109+
<form class="paste" action="fileupload" method="post" enctype="multipart/form-data">
110+
<div class='input'><input type="file" name="filetoupload"><div>
111+
<br>
112+
<input type="submit">
113+
</form>
114+
`)
115+
res.write(content);
116+
res.end();
117+
});
101118
}
102119
})
103120
.listen(parseInt(port), host);

templates/download.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Pastebin</title>
8+
<link
9+
rel="stylesheet"
10+
href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"
11+
/>
12+
<style>
13+
body {
14+
text-align: center;
15+
}
16+
17+
.paste {
18+
display: inline-block;
19+
margin: 0 auto;
20+
text-align: center;
21+
width: 50%;
22+
max-width: 800px;
23+
}
24+
25+
input {
26+
margin-left: auto;
27+
margin-right: auto;
28+
text-align: center;
29+
}
30+
</style>
31+
</head>
32+
<body>
33+
<h1>Pastebin</h1>
34+
<nav>
35+
<a href="/">Home</a>
36+
</nav>
37+
<br>
38+
<div>
39+
%CONTENT%
40+
</div>
41+
<br>
42+
</body>
43+
</html>

templates/home.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Pastebin</title>
8+
<link
9+
rel="stylesheet"
10+
href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"
11+
/>
12+
<style>
13+
body {
14+
text-align: center;
15+
}
16+
17+
.paste {
18+
display: inline-block;
19+
margin: 0 auto;
20+
text-align: center;
21+
width: 50%;
22+
max-width: 800px;
23+
}
24+
25+
input {
26+
margin-left: auto;
27+
margin-right: auto;
28+
text-align: center;
29+
}
30+
</style>
31+
</head>
32+
<body>
33+
<h1>Pastebin</h1>
34+
<nav>
35+
<a href="/download">Download Files</a>
36+
</nav>
37+
<br>
38+
<div>
39+
%CONTENT%
40+
</div>
41+
<br>
42+
</body>
43+
</html>

0 commit comments

Comments
 (0)