Closed basrioglumehmet closed 6 months ago
Where is the location of your static folder. It should be in the root not in src
I tested with this example and had no issues
import { Hono } from 'hono';
import { serveStatic } from "hono/bun";
class TestApp {
private readonly _instance: Hono;
get instance(): Hono {
return this._instance;
}
constructor() {
this._instance = new Hono();
this.registerRouters();
}
private registerRouters() {
this._instance.use('/static/*', serveStatic({ root: './',onNotFound(path, c) {
console.log(path)
}, }))
this._instance.get('/', (c) => {
return c.text('Hello World')
})
}
}
const app = new TestApp();
export default app.instance;
Where is the location of your static folder. It should be in the root not in
src
I tested with this example and had no issues
import { Hono } from 'hono'; import { serveStatic } from "hono/bun"; class TestApp { private readonly _instance: Hono; get instance(): Hono { return this._instance; } constructor() { this._instance = new Hono(); this.registerRouters(); } private registerRouters() { this._instance.use('/static/*', serveStatic({ root: './',onNotFound(path, c) { console.log(path) }, })) this._instance.get('/', (c) => { return c.text('Hello World') }) } } const app = new TestApp(); export default app.instance;
./ ├── favicon.ico
I did as you instructed and placed the source file in the root directory, but the result was still the same.
[!TIP] Therefore, I devised the following method, and it worked.
// this._instance.use('/static/*', serveStatic({ root: './',onNotFound(path, c) {
// console.log(path)
// }, }))
this._instance.use("/favicon.ico", async (c) => {
const file = Bun.file("./favicon.ico");
return new Response(file.stream(), {
headers: {
"Content-Type": "image/x-icon",
},
});
});
So it should be in a folder called static in the root, then it should work
So it should be in a folder called static in the root, then it should work
yep, it works now.And, my bad. I knew I should have paid more attention. Closed
To be fair @basrioglumehmet it could be clearer in the docs
To be fair @basrioglumehmet it could be clearer in the docs
yes, it could be more clear. Thanks for helping and explaining @NicoPlyley
Actually @basrioglumehmet the docs are completely wrong, I'll send an update now
What version of Hono are you using?
4.2.3
What runtime/platform is your app running on?
Bun
What steps can reproduce the bug?
Start the application. Attempt to access static files using the specified route /static/*.
What is the expected behavior?
Static files located in the root directory should be served successfully when accessed via the /static/* route.
What do you see instead?
Instead of successfully serving static files as expected, the application fails to serve the static content. Upon attempting to access static files through the specified route
/static/*
, the files are not served, and the expected content is not displayed.Additional information
Despite the inclusion of the
serveStatic
middleware with appropriate configuration, the static files are not served as expected. Additionally, no specific error message is provided, making it challenging to pinpoint the exact cause of the issue. Further investigation into the middleware configuration, potential conflicts, or errors during initialization may be necessary to resolve the problem with serving static files.