hono icon indicating copy to clipboard operation
hono copied to clipboard

`serveStatic` does not work when set absolute path to `root`

Open oon00b opened this issue 1 year ago • 1 comments

What version of Hono are you using?

4.4.12

What runtime/platform is your app running on?

Deno and Bun

What steps can reproduce the bug?

This is minimal script to reproduce the bug. This script can be executed with deno run --allow-net --allow-read command.

import { Hono } from "jsr:@hono/hono@^4.4.12";
import { serveStatic } from "jsr:@hono/hono@^4.4.12/deno";

const app = new Hono();

app.use(
    "*",
    serveStatic({
        root: "/tmp/public_html"
    })
);

Deno.serve(app.fetch);

The directory structure of /tmp/public_html/:

/tmp/public_html/
└── index.html

/tmp/public_html/index.html:

<p>test</p>

When accsessing http://localhost:8000, the server returns 404 Not Found and outputs an error:

NotFound: No such file or directory (os error 2): open './tmp/public_html/index.html'
NotFound: No such file or directory (os error 2): open './tmp/public_html/'

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

oon00b avatar Jul 07 '24 11:07 oon00b

+1

ckken avatar Jul 11 '24 03:07 ckken

Same here. Just tried on Bun. serveStatic doesn't serve file.

cj-1010-1414 avatar Sep 04 '24 04:09 cj-1010-1414

I am still not able to make it work. The referenced pull request talks about allowAbsoluteRoot which is nowhere to be found in the types. Guidance needed. Should this work or not?

acrodrig avatar Mar 14 '25 07:03 acrodrig

I still have the same Problem. allowAbsoluteRoot does not work.

In the code it looks like it determines automatically if the path is absolute by checking if it starts with /. That doesn't work for Windows paths!

thephluxer avatar Jul 09 '25 16:07 thephluxer

Hi @thephluxer

Is that a Windows-specific problem? If so, it will be fixed in the future by #3477. I want you to try the branch version of hono to confirm it works correctly. Can you take a look at the comment and try it?

https://github.com/honojs/hono/pull/3477#issuecomment-3031263813

yusukebe avatar Jul 09 '25 21:07 yusukebe