From 4e3691b88ed4951c4aceb9447e0071cf195405f6 Mon Sep 17 00:00:00 2001 From: Henrik L Date: Sun, 17 Mar 2024 16:26:46 +0100 Subject: [PATCH] unspecified --- index.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 7486a27..08daefe 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,29 @@ -const express = require('express') -const path = require('path') +const express = require("express") +const path = require("path") const app = express() -const rootPath = process.env.ROOT_PATH || '/resume' -const folderPath = path.join(__dirname, 'docs') +const rootPath = process.env.ROOT_PATH || "/resume" +const folderPath = path.join(__dirname, "docs") const port = process.env.PORT || 8080 app.use((req, res, next) => { - console.log(`Request received: ${req.method} ${req.url}`) - next() + console.log(`Request received: ${req.method} ${req.url}`) + next() }) -app.use(rootPath, express.static(path.join(__dirname, 'docs'))) +app.use(rootPath, express.static(path.join(__dirname, "docs"))) + +app.get("/:filename", (req, res, next) => { + const filename = req.params.filename + const filePath = path.join(staticFilesDirectory, filename) + + fs.access(filePath, fs.constants.F_OK, (err) => { + if (err) return next() + + res.sendFile(filePath) + }) +}) app.listen(port, () => { - console.log(`Server is running on port ${port}, hosting ${folderPath}`) + console.log(`Server is running on port ${port}, hosting ${folderPath}`) })