Skip to content

add support for absolute path to serveStatic root #187

Closed

Description

@charlyoleg2

Could you add support for absolute path for the root of the serveStatic?
In my use-case designix-uis, an absolute path to the folder containing the static files is required as tool can be called from any directory. Currently i"m using Express, but i"d like to replace it with Hono and this feature is missing.
The following patch in the file src/serve-static.ts might be sufficient for supporting the absolute path:

-      path = addCurrentDirPrefix(path)
+      if (!/^\//.test(path)) {
+        path = addCurrentDirPrefix(path)
+      }

Activity

  1. yusukebe commented on Aug 19, 2024

    @yusukebe
    Member

    Hi @charlyoleg2

    We may add supporting it.

    Related to: honojs/hono#3108 #78

  2. DoWhileGeek commented on Aug 22, 2024

    @DoWhileGeek

    @yusukebe I just ran into the absolute path issue as well 😅

  3. charlyoleg2 commented on Sep 28, 2024

    @charlyoleg2
    Author

    The hono's release 4.3.6 supports absolute path for serveStatic. Now node-server must forward the path without modifying it. This will enable the feature working in nodejs.
    The current release 1.13.1 of node-server prefixes the path with './'. This prevents the use of absolute path.

  4. siyant commented on Feb 13, 2025

    @siyant

    I'm developing a package that I expect people to run using "npx" from any directory too, it would be very helpful to be able to use absolute paths (or a relative path from the hono file, that would be even better).

    For now, I managed to work around this by getting the relative path from wherever the user is running it from, to my compiled node script. Then I use that as the root path for serving static files.

    // Hono expects the root/path in serveStatic to be relative to the working directory
    // where this file is being executed from. This finds the relative path from the working directory
    // to the parent folder of this file, then we can use it to point to the web build files.
    const __dirname = path.dirname(fileURLToPath(import.meta.url));
    const relativePathToScript = path.relative(process.cwd(), __dirname);
    
    // Serve web app (index.html at the root path "/", assets at "/_app/*" and favicon at "/favicon.png")
    app.get("/", serveStatic({ root: relativePathToScript, path: "web/index.html" }));
    app.get("/favicon.png", serveStatic({ root: relativePathToScript, path: "web/favicon.png" }));
    app.use("/_app/*", serveStatic({ root: `${relativePathToScript}/web` }))
    

    Note that if the relativePathToScript potentially needs to go up directories with ../ portions, it must be placed in the root param and not in the path param. I only realised this after a lot of confusion and frustration 🙈

    Hope this is helpful to anyone else with similar requirements!

  5. charlyoleg2 commented on Dec 27, 2025

    @charlyoleg2
    Author

    This feature is now implemented in node-server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions