Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: honojs/node-server
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.3.5
Choose a base ref
...
head repository: honojs/node-server
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.4.0
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Jan 1, 2024

  1. feat(serve-static): introduce onNotFound option (#119)

    * feat(serve-static): introduce `onNotFound` option
    
    * tweak
    yusukebe authored Jan 1, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    8a70dce View commit details
  2. v1.4.0

    yusukebe committed Jan 1, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    8cea466 View commit details
Showing with 40 additions and 3 deletions.
  1. +16 −0 README.md
  2. +1 −1 package.json
  3. +4 −2 src/serve-static.ts
  4. +19 −0 test/serve-static.test.ts
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -162,6 +162,22 @@ app.use(
)
```

#### `onNotFound`

The `onNotFound` is useful for debugging. You can write a handle for when a file is not found.

```ts
app.use(
'/static/*',
serveStatic({
root: './non-existent-dir',
onNotFound: (path, c) => {
console.log(`${path} is not found, request to ${c.req.path}`)
},
})
)
```

## Related projects

- Hono - <https://hono.dev>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hono/node-server",
"version": "1.3.5",
"version": "1.4.0",
"description": "Node.js Adapter for Hono",
"main": "dist/index.js",
"types": "dist/index.d.ts",
6 changes: 4 additions & 2 deletions src/serve-static.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReadStream} from 'fs'
import type { ReadStream } from 'fs'
import { createReadStream, existsSync, lstatSync } from 'fs'
import type { MiddlewareHandler } from 'hono'
import type { Context, MiddlewareHandler } from 'hono'
import { getFilePath } from 'hono/utils/filepath'
import { getMimeType } from 'hono/utils/mime'

@@ -12,6 +12,7 @@ export type ServeStaticOptions = {
path?: string
index?: string // default is 'index.html'
rewriteRequestPath?: (path: string) => string
onNotFound?: (path: string, c: Context) => void | Promise<void>
}

const createStreamBody = (stream: ReadStream) => {
@@ -51,6 +52,7 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew
path = `./${path}`

if (!existsSync(path)) {
await options.onNotFound?.(path, c)
return next()
}

19 changes: 19 additions & 0 deletions test/serve-static.test.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,17 @@ describe('Serve Static Middleware', () => {
})
)

let notFoundMessage = ''
app.use(
'/on-not-found/*',
serveStatic({
root: './not-found',
onNotFound: (path, c) => {
notFoundMessage = `${path} is not found, request to ${c.req.path}`
},
})
)

const server = createAdaptorServer(app)

it('Should return index.html', async () => {
@@ -110,4 +121,12 @@ describe('Serve Static Middleware', () => {
expect(res.text.length).toBe(17)
expect(res.text).toBe('This is plain.txt')
})

it('Should handle the `onNotFound` option', async () => {
const res = await request(server).get('/on-not-found/foo.txt')
expect(res.status).toBe(404)
expect(notFoundMessage).toBe(
'./not-found/on-not-found/foo.txt is not found, request to /on-not-found/foo.txt'
)
})
})