Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

docs(server): add nitro config and storage examples #6507

Merged
merged 8 commits into from
Aug 15, 2022
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 86 additions & 0 deletions docs/content/2.guide/2.features/9.server-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ export default defineEventHandler((event) => {

## Advanced Usage Examples

### Nitro Configuration

You can use `nitro` key in `nuxt.config` to directly set [Nitro Configuration](https://nitro.unjs.io/config/).

::alert{type=warning}
This is an advanced option. Custom config can affect production deployments and we upgrade Nitro in semver-minor versions of Nuxt 3. meaning, configuration interface might be changed over the time.
::

```ts [nuxt.config.ts]
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
// https://nitro.unjs.io/config
nitro: {}
})
```

### Using a nested router

```ts [/server/api/hello.ts]
Expand Down Expand Up @@ -209,3 +226,72 @@ export default (req, res, next) => {
::alert{type=warning}
Never combine `next()` callback with a legacy middleware that is `async` or returns a `Promise`!
::

### Server Storage

Nitro Provides a cross platform [Stroage Layer](https://nitro.unjs.io/guide/storage.html). In oder to configure additional storage mountpoints, you can use `nitro.storage`

#### Example: Using Redis

```ts [nuxt.config.ts]
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
nitro: {
storage: {
'redis': {
driver: 'redis',
/* redis connector options */
port: 6379, // Redis port
host: "127.0.0.1", // Redis host
username: "", // needs Redis >= 6
password: "",
db: 0, // Defaults to 0
},
}
}
})
```

Create a new file in `server/api/test.post.ts`:

```ts [/server/api/test.post.ts]
export default async (req, res) => {
const body = await useBody(req)
await useStorage().setItem('redis:nuxt3-redis', body)
return 'Success'
}
```

Create a new file in `server/api/test.get.ts`:

```ts [/server/api/test.get.ts]
export default async (req, res) => {
const data = await useStorage().getItem('redis:nuxt3-redis')
return data
}
```

Create a new file in `app.vue`:

```vue [app.vue]
<template>
<div>
<div>Post state: {{resDataSuccess}}</div>
<div>Get Data: {{ resData.text }}</div>
</div>
</template>

<script setup lang="ts">
const { data: resDataSuccess } = await useFetch(
'/api/test',
{
method: 'post',
body: {
text: 'Welcome To Nuxt3'
}
}
)
const { data: resData } = await useFetch('/api/test')
</script>
```