diff --git a/src/serve-static.ts b/src/serve-static.ts index 9cf9dc8..a2c2dd0 100644 --- a/src/serve-static.ts +++ b/src/serve-static.ts @@ -1,7 +1,7 @@ import type { Context, Env, MiddlewareHandler } from 'hono' import { getMimeType } from 'hono/utils/mime' import type { ReadStream, Stats } from 'node:fs' -import { createReadStream, lstatSync } from 'node:fs' +import { createReadStream, lstatSync, existsSync } from 'node:fs' import { join } from 'node:path' export type ServeStaticOptions = { @@ -59,6 +59,10 @@ export const serveStatic = ( const root = options.root || '' const optionPath = options.path + if (root !== '' && !existsSync(root)) { + console.error(`serveStatic: root path '${root}' is not found, are you sure it's correct?`) + } + return async (c, next) => { // Do nothing if Response is already set if (c.finalized) { diff --git a/test/serve-static.test.ts b/test/serve-static.test.ts index a0fa603..af93429 100644 --- a/test/serve-static.test.ts +++ b/test/serve-static.test.ts @@ -325,3 +325,27 @@ describe('Serve Static Middleware', () => { }) }) }) + +describe('Serve Static Middleware with wrong path', () => { + it('Should show an error when the path is wrong', async () => { + const logSpy = jest.spyOn(console, 'error') + + const app = new Hono<{ + Variables: { + path: string + } + }>() + + app.use( + '*', + serveStatic({ + root: './public', + }) + ) + + expect(logSpy).toHaveBeenCalledWith( + // eslint-disable-next-line quotes + "serveStatic: root path './public' is not found, are you sure it's correct?" + ) + }) +})