Skip to content

Commit

Permalink
update README with CWD behavior for serve:false option
Browse files Browse the repository at this point in the history
  • Loading branch information
nimesh0505 committed Sep 3, 2024
1 parent 387499c commit 64bc7f8
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,36 @@ Assume this structure with the compressed asset as a sibling of the un-compresse
└── index.html
```

#### Disable serving
#### Disable serving and CWD behavior

If you would just like to use the reply decorator and not serve whole directories automatically, you can simply pass the option `{ serve: false }`. This will prevent the plugin from serving everything under `root`.

When `serve: false` is passed:

1. The plugin will not perform the usual directory existence check for the `root` option.
2. If no `root` is provided, the plugin will default to using the current working directory (CWD) as the root.
3. A warning will be logged if no `root` is provided, informing that the CWD is being used as the default.

Example usage:

```js
const fastify = require('fastify')()
const path = require('node:path')

fastify.register(require('@fastify/static'), {
serve: false,
// root is optional when serve is false, will default to CWD if not provided
root: path.join(__dirname, 'public')
})

fastify.get('/file', (req, reply) => {
// This will serve the file from the CWD if no root was provided
reply.sendFile('myFile.html')
})
```

This configuration allows you to use the `sendFile` decorator without automatically serving an entire directory, giving you more control over which files are accessible.

#### Disabling reply decorator

The reply object is decorated with a `sendFile` function by default. If you want to
Expand Down

0 comments on commit 64bc7f8

Please sign in to comment.