diff --git a/docs/2.guide/2.directory-structure/1.server.md b/docs/2.guide/2.directory-structure/1.server.md index edbde12713..02b72bcd9e 100644 --- a/docs/2.guide/2.directory-structure/1.server.md +++ b/docs/2.guide/2.directory-structure/1.server.md @@ -340,10 +340,12 @@ Never combine `next()` callback with a legacy middleware that is `async` or retu ### Server Storage -Nitro provides a cross-platform [storage layer](https://nitro.unjs.io/guide/storage). In order to configure additional storage mount points, you can use `nitro.storage`. +Nitro provides a cross-platform [storage layer](https://nitro.unjs.io/guide/storage). In order to configure additional storage mount points, you can use `nitro.storage`, or [server plugins](#server-plugins). #### Example: Using Redis +Using `nitro.storage`: + ```ts [nuxt.config.ts] export default defineNuxtConfig({ nitro: { @@ -363,6 +365,43 @@ export default defineNuxtConfig({ }) ``` +Alternatively, using server plugins: + +::code-group + +```ts [server/plugins/storage.ts] +import redisDriver from 'unstorage/drivers/redis' + +export default defineNitroPlugin(() => { + const storage = useStorage() + + // Dynamically pass in credentials from runtime configuration, or other sources + const driver = redisDriver({ + base: 'redis', + host: useRuntimeConfig().redis.host, + port: useRuntimeConfig().redis.port, + /* other redis connector options */ + }) + + // Mount driver + storage.mount('redis', driver) +}) +``` + +``` ts [nuxt.config.ts] +export default defineNuxtConfig({ + runtimeConfig: { + redis: { // Default values + host: '', + port: 0, + /* other redis connector options */ + } + } +}) +``` + +:: + Create a new file in `server/api/test.post.ts`: ```ts [server/api/test.post.ts]