mirror of
https://github.com/honojs/hono.git
synced 2024-11-29 17:46:30 +01:00
1cdd71f676
* feat: add `serve-static` middlware for deno * make `getFilePath` into utils * ignore deno ts files
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import type { Context } from '../context.ts'
|
|
import type { Next } from '../hono.ts'
|
|
import { getFilePath } from '../utils/filepath.ts'
|
|
import { getMimeType } from '../utils/mime.ts'
|
|
|
|
export type ServeStaticOptions = {
|
|
root?: string
|
|
path?: string
|
|
}
|
|
|
|
const DEFAULT_DOCUMENT = 'index.html'
|
|
|
|
export const serveStatic = (options: ServeStaticOptions = { root: '' }) => {
|
|
return async (c: Context, next: Next): Promise<Response | undefined> => {
|
|
// Do nothing if Response is already set
|
|
if (c.res && c.finalized) {
|
|
await next()
|
|
}
|
|
|
|
const url = new URL(c.req.url)
|
|
|
|
let path = getFilePath({
|
|
filename: options.path ?? url.pathname,
|
|
root: options.root,
|
|
defaultDocument: DEFAULT_DOCUMENT,
|
|
})
|
|
|
|
path = `./${path}`
|
|
const content = await Deno.readFile(path)
|
|
if (content) {
|
|
const mimeType = getMimeType(path)
|
|
if (mimeType) {
|
|
c.header('Content-Type', mimeType)
|
|
}
|
|
// Return Response object
|
|
return c.body(content)
|
|
} else {
|
|
console.warn(`Static file: ${path} is not found`)
|
|
await next()
|
|
}
|
|
return
|
|
}
|
|
}
|