From 486f1bbe318f2f509df9ac8291e78335a846a015 Mon Sep 17 00:00:00 2001 From: HomWang <516310460@qq.com> Date: Thu, 11 Aug 2022 17:48:17 +0800 Subject: [PATCH 1/6] If nitro can be used. Please add Added the usage of nitro to help those who want to use server storage --- .../3.directory-structure/13.server.md | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/docs/content/2.guide/3.directory-structure/13.server.md b/docs/content/2.guide/3.directory-structure/13.server.md index d442f079b2e..a896f8dd575 100644 --- a/docs/content/2.guide/3.directory-structure/13.server.md +++ b/docs/content/2.guide/3.directory-structure/13.server.md @@ -7,3 +7,72 @@ head.title: Server directory # Server directory ::ReadMore{link="/guide/features/server-routes"} + +## nitro + +Create a new file in `nitro.config.ts`: + +```ts [nitro.config.ts] +import { defineNitroConfig } from 'nitropack' + +export default defineNitroConfig({ + 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 + }, + } +}) +``` + +:LinkExample{link="https://nitro.unjs.io/guide/storage.html#defining-mountpoints"} + +## Example + +Create a new file in `server/api/test.post.ts`: + +```ts [/server/api/test.post.ts] +export default async (req, res) => { + await useStorage().setItem('redis:nuxt3-redis', { hello: 'wallet' }) + 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] + + + + + Post state: {{resDataSuccess}} + Get Data: {{ resData.text }} + + +``` + From 521e3669cc9b2eeb302b06af7a6c5f85bbb2bd56 Mon Sep 17 00:00:00 2001 From: HomWang <516310460@qq.com> Date: Thu, 11 Aug 2022 18:04:14 +0800 Subject: [PATCH 2/6] Modify nitro in nuxt.config.ts Modify `nitro` in nuxt.config.ts --- .../3.directory-structure/13.server.md | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/docs/content/2.guide/3.directory-structure/13.server.md b/docs/content/2.guide/3.directory-structure/13.server.md index a896f8dd575..02e8bd3a76c 100644 --- a/docs/content/2.guide/3.directory-structure/13.server.md +++ b/docs/content/2.guide/3.directory-structure/13.server.md @@ -10,22 +10,24 @@ head.title: Server directory ## nitro -Create a new file in `nitro.config.ts`: - -```ts [nitro.config.ts] -import { defineNitroConfig } from 'nitropack' - -export default defineNitroConfig({ - 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 `nuxt.config.ts`: + +```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 + }, + } } }) ``` @@ -55,6 +57,13 @@ export default async (req, res) => { Create a new file in `app.vue`: ```vue [app.vue] + + + Post state: {{resDataSuccess}} + Get Data: {{ resData.text }} + + + - - - - Post state: {{resDataSuccess}} - Get Data: {{ resData.text }} - - ``` - From 40eb56f0f000588785b3a1dabd5b30a36c61e9ee Mon Sep 17 00:00:00 2001 From: HomWang <516310460@qq.com> Date: Fri, 12 Aug 2022 17:59:40 +0800 Subject: [PATCH 3/6] The request adopts the 'body' method The request adopts the 'body' method --- docs/content/2.guide/3.directory-structure/13.server.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/2.guide/3.directory-structure/13.server.md b/docs/content/2.guide/3.directory-structure/13.server.md index 02e8bd3a76c..49f4a0c4562 100644 --- a/docs/content/2.guide/3.directory-structure/13.server.md +++ b/docs/content/2.guide/3.directory-structure/13.server.md @@ -40,7 +40,7 @@ Create a new file in `server/api/test.post.ts`: ```ts [/server/api/test.post.ts] export default async (req, res) => { - await useStorage().setItem('redis:nuxt3-redis', { hello: 'wallet' }) + await useStorage().setItem('redis:nuxt3-redis', req.body) return 'Success' } ``` From 3ed77c7716d0fdb411b64e411db6bdb80e0eb7b9 Mon Sep 17 00:00:00 2001 From: HomWang <516310460@qq.com> Date: Fri, 12 Aug 2022 18:18:16 +0800 Subject: [PATCH 4/6] Add useBody Use useBody() --- docs/content/2.guide/3.directory-structure/13.server.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/content/2.guide/3.directory-structure/13.server.md b/docs/content/2.guide/3.directory-structure/13.server.md index 49f4a0c4562..113e73add5a 100644 --- a/docs/content/2.guide/3.directory-structure/13.server.md +++ b/docs/content/2.guide/3.directory-structure/13.server.md @@ -40,7 +40,8 @@ Create a new file in `server/api/test.post.ts`: ```ts [/server/api/test.post.ts] export default async (req, res) => { - await useStorage().setItem('redis:nuxt3-redis', req.body) + const body = await useBody(req) + await useStorage().setItem('redis:nuxt3-redis', body) return 'Success' } ``` From 84dce76f4ec2e2a9aaec00d9d10513cc9af1514c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 15 Aug 2022 16:23:09 +0200 Subject: [PATCH 5/6] refactor: move to server-routes --- .../2.guide/2.features/9.server-routes.md | 86 +++++++++++++++++++ .../3.directory-structure/13.server.md | 71 --------------- 2 files changed, 86 insertions(+), 71 deletions(-) diff --git a/docs/content/2.guide/2.features/9.server-routes.md b/docs/content/2.guide/2.features/9.server-routes.md index fbecf165db8..1c6b9cb4b42 100644 --- a/docs/content/2.guide/2.features/9.server-routes.md +++ b/docs/content/2.guide/2.features/9.server-routes.md @@ -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] @@ -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] + + + Post state: {{resDataSuccess}} + Get Data: {{ resData.text }} + + + + +``` diff --git a/docs/content/2.guide/3.directory-structure/13.server.md b/docs/content/2.guide/3.directory-structure/13.server.md index 113e73add5a..d442f079b2e 100644 --- a/docs/content/2.guide/3.directory-structure/13.server.md +++ b/docs/content/2.guide/3.directory-structure/13.server.md @@ -7,74 +7,3 @@ head.title: Server directory # Server directory ::ReadMore{link="/guide/features/server-routes"} - -## nitro - -Create a new file in `nuxt.config.ts`: - -```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 - }, - } - } -}) -``` - -:LinkExample{link="https://nitro.unjs.io/guide/storage.html#defining-mountpoints"} - -## Example - -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] - - - Post state: {{resDataSuccess}} - Get Data: {{ resData.text }} - - - - -``` From 1814ddd2d16a95924074162f4558c02646395ea9 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 15 Aug 2022 16:28:22 +0200 Subject: [PATCH 6/6] update examples --- .../2.guide/2.features/9.server-routes.md | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/docs/content/2.guide/2.features/9.server-routes.md b/docs/content/2.guide/2.features/9.server-routes.md index dbf02333735..ab0c4729168 100644 --- a/docs/content/2.guide/2.features/9.server-routes.md +++ b/docs/content/2.guide/2.features/9.server-routes.md @@ -229,7 +229,7 @@ Never combine `next()` callback with a legacy middleware that is `async` or retu ### 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` +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 @@ -256,20 +256,20 @@ export default defineNuxtConfig({ 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' -} +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 (req, res) => { - const data = await useStorage().getItem('redis:nuxt3-redis') +export default async defineEventHandler(event => { + const data = await useStorage().getItem('redis:test') return data -} +}) ``` Create a new file in `app.vue`: @@ -277,21 +277,16 @@ Create a new file in `app.vue`: ```vue [app.vue] - Post state: {{resDataSuccess}} + Post state: {{ resDataSuccess }} Get Data: {{ resData.text }} ```