Closed Hmoulvad closed 1 month ago
Same here. I've been trying with both nodejs and bun adapters but the static folder does not seem to be actually served. I tried the same thing with ElysiaJS and it works just fine.
Hi @Hmoulvad
Please provide the "minimal" project to reproduce it.
I had some problem with this also, if you add console.log("Path: ", path)
after the path variable on line 22 in /adapter/bun/serve-static.ts
What does it say?
Where and how are you launching bun from?`
For me the problem was where I was launching bun from.
@Hmoulvad
Personally, this is how I use serveStatic
app.use('/styles/*', serveStatic({
root: 'public/',
onNotFound: (path, c) => {
console.log(`${path} is not found, you access ${c.req.path}`)
}
}));
This allows me to access the styles in /public/styles/style.css
with /styles/style.css
. The onNotFound
function also helps me debug in real-time.
I personally don't think it's intuitive but any change to it would qualify as a breaking change, so I assume it'd stay this way for a bit longer.
Just find the solution in my case
The root
is always relative to the bun
execution path.
Meaning if you want to server static the file and folder should be accessible in the path relative to bun
.
So if you ran
$usr/serve/app/: bun src/index.ts
And in your code you wrote
app.use('/dashboard/*', serveStatic({ root: 'frontend'}));
Then the files need to be in usr/serve/app/frontend/dashboard
@byawitz You save me, thanks.
I just want to extend off of the previous answer; I wanted my app to always serve files relative to the script, not the current working directory. Normally I'd just use __dirname
or import.meta.dirname
for this, but I had trouble getting this working. Perhaps due to something here or here?
Regardless, for now, my workaround was to use path.relative()
to get the relative path between the current working directory and the file directory.
const relativePathToScript = path.relative(process.cwd(), __dirname);
app.use('/static/*', serveStatic({ root: relativePathToScript }))
for me it was mostly solved by manually starting hono server, ig it really matter where you start your server from.
Same here. Just tried on Bun. serveStatic
doesn't serve file.
Related to https://github.com/honojs/hono/pull/3420
This was closed by #3420. Thanks.
What version of Hono are you using?
3.12.8
What runtime/platform is your app running on?
Bun
What steps can reproduce the bug?
I am trying to recreate the example: https://hono.dev/getting-started/bun#serve-static-files but I can't seem to get it working.
What is the expected behavior?
That I can access the static files provided with the
serverStatic
functionWhat do you see instead?
I am getting status 404 whatever I try.
Additional information
Trying to access it from RootLayout:
This is my structure: