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

Commit

Permalink
docs(server): add nitro config and storage examples (#6507)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
516310460 and pi0 committed Aug 15, 2022
1 parent 676514d commit 3920065
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 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,67 @@ 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 defineEventHandler(async event => {
const body = await useBody(event)
await useStorage().setItem('redis:test', body)
return 'Data is set'
})
```

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

```ts [/server/api/test.get.ts]
export default async defineEventHandler(event => {
const data = await useStorage().getItem('redis:test')
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: 'Nuxt is Awesome!' }
})
const { data: resData } = await useFetch('/api/test')
</script>
```

0 comments on commit 3920065

Please sign in to comment.