From 64bc7f8999e5d2b79ce42f996ad5be914cf65f28 Mon Sep 17 00:00:00 2001 From: nimesh0505 Date: Tue, 3 Sep 2024 15:04:35 +0530 Subject: [PATCH] update README with CWD behavior for serve:false option --- README.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c52fa9b..0a99ab4 100644 --- a/README.md +++ b/README.md @@ -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