Basic Function

This commit is contained in:
bingus_violet 2024-01-16 18:27:26 +00:00
parent 5bf43f9b4c
commit d118811edd
2 changed files with 47 additions and 0 deletions

View file

@ -6,7 +6,42 @@ var app = express()
var PORT = process.env.PORT || 8080 var PORT = process.env.PORT || 8080
var directory = process.env.FILES_DIR
var baseHTML = fs.readFileSync(path.join(__dirname, 'resources/base.html')).toString()
var baseStart = baseHTML.substring(0, baseHTML.indexOf('{CONTENT}'))
var baseEnd = baseHTML.substring(baseHTML.indexOf('{CONTENT}') + 9, baseHTML.length)
app.use(express.static(directory))
if (!directory) {
console.error("No directory specified! Please specify one using the environment variable FILES_DIR.")
return
}
app.listen(PORT, () => { app.listen(PORT, () => {
console.log("Now listening on PORT: " + PORT) console.log("Now listening on PORT: " + PORT)
}) })
app.get("/*", (req, res) => {
var file = req.params[0]
var absPath = path.join(directory, file)
try {
var dirContents = fs.readdirSync(absPath)
} catch (error) {
res.send("<p>404: not found!</p>")
}
res.write(baseStart)
res.write('<a href="../">Parent Directory</a><br>')
for (let index = 0; index < dirContents.length; index++) {
const file = dirContents[index];
res.write(`<a href="./${file}">${file}</a><br>`)
}
res.write(baseEnd)
res.end()
})

12
resources/base.html Normal file
View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{CONTENT}
</body>
</html>