Skip to content

Commit 69c7240

Browse files
feat: add log when directory does not exists (#273)
* feat: add log when directory does not exists Signed-off-by: n4n5 <[email protected]> * fmt * lint * add a comment for disabling the lint --------- Signed-off-by: n4n5 <[email protected]>
1 parent e3059dd commit 69c7240

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/serve-static.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Context, Env, MiddlewareHandler } from 'hono'
22
import { getMimeType } from 'hono/utils/mime'
33
import type { ReadStream, Stats } from 'node:fs'
4-
import { createReadStream, lstatSync } from 'node:fs'
4+
import { createReadStream, lstatSync, existsSync } from 'node:fs'
55
import { join } from 'node:path'
66

77
export type ServeStaticOptions<E extends Env = Env> = {
@@ -59,6 +59,10 @@ export const serveStatic = <E extends Env = any>(
5959
const root = options.root || ''
6060
const optionPath = options.path
6161

62+
if (root !== '' && !existsSync(root)) {
63+
console.error(`serveStatic: root path '${root}' is not found, are you sure it's correct?`)
64+
}
65+
6266
return async (c, next) => {
6367
// Do nothing if Response is already set
6468
if (c.finalized) {

test/serve-static.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,27 @@ describe('Serve Static Middleware', () => {
325325
})
326326
})
327327
})
328+
329+
describe('Serve Static Middleware with wrong path', () => {
330+
it('Should show an error when the path is wrong', async () => {
331+
const logSpy = jest.spyOn(console, 'error')
332+
333+
const app = new Hono<{
334+
Variables: {
335+
path: string
336+
}
337+
}>()
338+
339+
app.use(
340+
'*',
341+
serveStatic({
342+
root: './public',
343+
})
344+
)
345+
346+
expect(logSpy).toHaveBeenCalledWith(
347+
// eslint-disable-next-line quotes
348+
"serveStatic: root path './public' is not found, are you sure it's correct?"
349+
)
350+
})
351+
})

0 commit comments

Comments
 (0)