-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serving static assets from a specific directory #2
Comments
Hello, Anyone looking for a simple solution, here is my code to load static assets from a folder called import { readFileSync, readdirSync, statSync } from "fs"
import path from "path"
// Function to get files from their directory recursively.
const getFiles = (dirName) => {
let files = []
const items = readdirSync(dirName, { withFileTypes: true })
for (const item of items) {
if (item.isDirectory()) {
files = [...files, ...getFiles(`${dirName}/${item.name}`)]
} else {
files.push(`${dirName}/${item.name}`)
}
}
return files
}
// Function to support different content types
const getContentType = (file) => {
const extname = path.extname(file)
if (extname === ".css") {
return "text/css"
} else if (extname === ".js") {
return "application/javascript"
} else if (extname === ".png") {
return "image/png"
} else if (extname === ".jpg" || extname === ".jpeg") {
return "image/jpeg"
} else if (extname === ".gif") {
return "image/gif"
} else if (extname === ".avif") {
return "image/avif"
}
return "application/octet-stream" // Default to binary data if the content type is not recognized
}
const staticAssets = getFiles("static")
staticAssets.forEach((el) => {
app.get(`/${el}`, (req, res) => {
const filePath = path.join(process.cwd(), `/${el}`)
try {
const stats = statSync(filePath)
if (stats.isFile()) {
const contentType = getContentType(filePath)
res.setHeader("Content-Type", contentType)
const fileContents = readFileSync(filePath)
res.end(fileContents)
} else {
// If it's not a file, send a 404 Not Found response.
res.end("Not Found")
}
} catch (err) {
// Handle any potential errors.
console.error(`Error while serving file: ${err.message}`)
res.end("Internal Server Error")
}
})
}) Hope this will be useful. |
Hi @LebCit, You may check the current progress at Learn Node.js the Hard Way |
Hi @ishtms app.get("/:folder/:filename", async (req, res) => {...}) where the app.get("/posts", async (req, res) => {---}) The Velocy is great, straightforward, easy to use, without any dependency and lightweight. |
Hello,
Thank you very much for your time and efforts to bring this minimalist and effective Node.js framework.
How to serve static assets like CSS, JS and images files from a specific directory using Velocy ?
Consider the following route:
And
test.html
:As you can see I want to load
main.css
from thestyles
folder which is in thestatic
folder.The idea is to have a way to load static assets from a specific folder like other frameworks.
Thanks a lot again for your time and efforts.
SYA
The text was updated successfully, but these errors were encountered: