GitHub 37
Skip to content

First Server Primitive

Add local KV to your app, save a setting, and return the stored value from a route.

Save a theme setting with KV and read it back in a server route. KV stores each value under a key. This tutorial uses local files, so you need no provider account or credentials.

Complete Installation, check /api/health, and continue in that app with Node.js 24.15 or newer and pnpm.

Prefer to skip the setup? Run this tutorial in your browser with a live editor and preview.

Install the KV package:

commands/install-kv
pnpm add @vite-hub/kv

Add kv: true and keep your other options. On the node host, KV stores values in files under .vitehub/data/kv.

Nuxt
nuxt.config.ts
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    kv: true,
  },
})
Nitro
vite.config.ts
import { defineConfig } from "vite"
import { nitro } from 'nitro/vite'
import { vitehub } from "vite-hub"

export default defineConfig({
  plugins: [
    vitehub({
      kv: true,
    }),
    nitro(),
  ],
})
Vite
vite.config.ts
import { defineConfig } from "vite"
import { vitehub } from "vite-hub"

export default defineConfig({
  plugins: [
    vitehub({
      kv: true,
    }),
  ],
})

Create server/api/settings.post.ts. Nitro and Nuxt serve it at POST /api/settings. It saves the request body under the key settings, reads it back, and returns it.

KV calls return [error, value]. Check the error before you use the value. kv.set() returns no value, so the route only checks writeError.

server/api/settings.post.ts
import { defineEventHandler, readBody } from "h3"
import { kv } from "vite-hub/kv"

export default defineEventHandler(async (event) => {
  const settings = await readBody<{ theme: string }>(event)

  const [writeError] = await kv.set("settings", settings)
  if (writeError) throw writeError

  const [readError, storedSettings] = await kv.get("settings")
  if (readError) throw readError
  return { settings: storedSettings }
})

Explicit H3 imports make the example work without Nuxt auto-imports.

Restart your app to load the new configuration:

commands/dev
pnpm dev

In another terminal, send the setting:

commands/request
curl -X POST http://localhost:3000/api/settings \
  -H 'content-type: application/json' \
  -d '{"theme":"system"}'

You should receive:

output/response.json
{"settings":{"theme":"system"}}

Returning the result of kv.get() proves both the write and read worked. Send "theme":"dark" to replace the stored value.

To check the production server of the Vite app, stop pnpm dev, build and start the server, and repeat the request:

commands/build
pnpm build
pnpm start

In Nuxt, run pnpm build and node .output/server/index.mjs instead.

Values survive a restart as long as .vitehub/data/kv exists on disk. Do not commit .vitehub. A deployed server needs persistent storage or a hosted KV provider. Changing the driver does not move existing data.

Read KV configuration to pick a hosted store or add a named store. The KV server API lists method results and errors. For other data, try the Database tutorial or Blob tutorial.