honoで Serve static files を参照して設定していたところ、少しハマっていたのでメモ。
// /src/index.ts
const app = new Hono().basePath("/api");
app.use("/static/[]()*", serveStatic({ root: "./" }));├── src
│ ├── domain
│ ├── infrastructure
│ ├── routes
│ └── use-case
└── static
└── imagesこのような状況でstatic/image/file.xxxがNot Foundになって困っていました。
どうやらbasePathがstaticPathにも影響するらしく、staticPathのmiddlewareの設定以前にbasePath設定があると影響するようでした。
// /src/index.ts
const app = new Hono();
app.use("/static/*", serveStatic({ root: "./" }));
app.basePath("/api");このようにmiddleware以降にbasePath設定をすることでstaticPathへのアクセスが適切にできるようになりました。
serveStaticのrootの対象がどこからなのかわからないこともあって、結構時間が溶けました。。。
ちなみに、
Root path, relative to current working directory from which the app was started. Absolute paths are not supported.
コードのコメント上にある通り、root pathはindex.ts(ここでいうsrc/パス)のディレクトリではなく、プロセスの起動しているディレクトリであるproject root pathになるので注意。