Skip to content

Commit

Permalink
Update readme with an example of proper "root" when using serveStatic (
Browse files Browse the repository at this point in the history
…#173)

* Update readme with an example of proper "root" when using serveStatic
Update README with example of correct "root" for serveStatic

Also update the ts doc string for this parameter to be clearer

* Tweak README example for serveStatic a little more
  • Loading branch information
brettimus authored Jun 8, 2024
1 parent 07dbb23 commit e3b1f82
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,28 @@ import { serveStatic } from '@hono/node-server/serve-static'
app.use('/static/*', serveStatic({ root: './' }))
```

Note that `root` must be _relative_ to the current working directory - absolute paths are not supported.
Note that `root` must be _relative_ to the current working directory from which the app was started. Absolute paths are not supported.

This can cause confusion when running your application locally.

Imagine your project structure is:

```
my-hono-project/
src/
index.ts
static/
index.html
```

Typically, you would run your app from the project's root directory (`my-hono-project`),
so you would need the following code to serve the `static` folder:

```ts
app.use('/static/*', serveStatic({ root: './static' }))
```

Notice that `root` here is not relative to `src/index.ts`, rather to `my-hono-project`.

### Options

Expand Down
2 changes: 1 addition & 1 deletion src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getMimeType } from 'hono/utils/mime'

export type ServeStaticOptions = {
/**
* Root path, relative to current working directory. (absolute paths are not supported)
* Root path, relative to current working directory from which the app was started. Absolute paths are not supported.
*/
root?: string
path?: string
Expand Down

0 comments on commit e3b1f82

Please sign in to comment.