const express = require("express"), fs = require("fs"), path = require("path") var app = express() var PORT = process.env.PORT || 8080 var directory = process.env.FILES_DIR var passwd = process.env.PASSWORD 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) if (!directory) { console.error("No directory specified! Please specify one using the environment variable FILES_DIR.") return } app.use('/b', express.static(directory)) app.use(express.static(path.join(__dirname, 'static'))) app.listen(PORT, () => { console.log("Now listening on PORT: " + PORT) if (!passwd) { console.log("Password variable not found. Uploading from site is not available.") } else { console.log("Password variable found! Uploads are now available on the site.") } }) app.get("/b*", (req, res) => { var file = req.params[0] var absPath = path.join(directory, file) res.setHeader('Content-Type', 'text/html') try { var dirContents = fs.readdirSync(absPath) } catch (error) { res.send(baseStart + "

404: not found!

" + baseEnd) return } res.write(baseStart) res.write('Parent Directory
') for (let index = 0; index < dirContents.length; index++) { const file = dirContents[index]; res.write(`${file}
`) } res.write(baseEnd) res.end() }) app.get("/", (req, res) => { res.redirect("/b") })