|
36 | 36 | // If request url is '/download' |
37 | 37 | else if (req.url == "/download") { |
38 | 38 | // Display links to download files |
| 39 | + |
| 40 | + let html = `<ul>` |
39 | 41 | fs.readdir("./uploads", function (err, files) { |
40 | 42 | if (err) throw err; |
41 | 43 | 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>` |
43 | 45 | }); |
44 | | - res.end(); |
45 | 46 | }); |
| 47 | + html += `</ul>` |
46 | 48 |
|
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 | + }); |
48 | 58 | } else if (req.url !== "/") { |
49 | 59 | // parse URL |
50 | 60 | const parsedUrl = url.parse(req.url); |
@@ -90,14 +100,21 @@ http |
90 | 100 | }); |
91 | 101 | } else { |
92 | 102 | 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 | + }); |
101 | 118 | } |
102 | 119 | }) |
103 | 120 | .listen(parseInt(port), host); |
|
0 commit comments