Building Full-Stack Apps with Nuxt 4 and Vue 3: SSR, Auto-imports, and Nitro Server for Production

Programming tutorial - IT technology blog
Programming tutorial - IT technology blog

What Nuxt 4 Actually Solves

At some point in a custom Vue 3 setup — Vite-based or otherwise — you hit the same wall. The router works, but now you need SSR. SSR works, but now you need an API layer. Suddenly you are maintaining three separate concerns. Nuxt 4 collapses them into one.

Three things matter most for anyone moving into full-stack territory:

  • Server-Side Rendering (SSR) — pages render on the server before the browser receives anything. First-paint is faster, and search engines index actual content instead of a blank shell. No configuration required to get started.
  • Auto-imports — Vue composables, components, and utilities are available everywhere without a single import statement. Files stay focused. Copy-paste errors from missing imports disappear entirely.
  • Nitro server — an embedded server engine that handles both SSR and your own API routes. Drop a file under server/api/ and Nitro exposes it as a REST endpoint. No router setup, no Express config.

Running this stack across two production sites, TTFB dropped from around 800ms to under 200ms compared to a client-rendered Vue SPA. The codebase shrunk too — no separate backend service needed for lightweight data fetching.

This guide takes you from a blank terminal to a running production build. No prior Nuxt experience assumed.

Installation

Prerequisites

Nuxt 4 requires Node.js 20 or higher. Check your version first:

node -v
# Should output v20.x.x or higher

On an older version? Use nvm to upgrade without touching other projects:

nvm install 20
nvm use 20

Scaffolding the Project

The official Nuxt CLI sets up everything in one command — Vite, Vue 3, TypeScript support, and the Nitro server included:

npx nuxi@latest init my-nuxt-app
cd my-nuxt-app
npm install

The setup wizard asks which package manager to use. Both npm and pnpm work fine. For production, pnpm is worth switching to — installs run roughly 2x faster and node_modules takes about half the disk space compared to npm on the same project.

# Optional: use pnpm instead
npm install -g pnpm
pnpm install

Project Structure Overview

A fresh scaffold looks like this:

my-nuxt-app/
├── app.vue          # Root Vue component
├── nuxt.config.ts   # Main Nuxt configuration
├── pages/           # File-based routing
├── components/      # Auto-imported Vue components
├── composables/     # Auto-imported composables
├── server/
│   └── api/         # Nitro API routes
└── public/          # Static assets

Each folder has a specific role. The critical point: Nuxt reads these directories automatically. Routes register themselves, components import themselves, and API endpoints appear the moment you create the file.

Configuration

Enabling and Tuning SSR

SSR is on by default. Open nuxt.config.ts to confirm and add basic head metadata:

// nuxt.config.ts
export default defineNuxtConfig({
  ssr: true,  // Already true by default

  app: {
    head: {
      title: 'My Nuxt App',
      meta: [
        { name: 'description', content: 'Full-stack app built with Nuxt 4' }
      ]
    }
  }
})

Some pages don’t need SSR — a user dashboard behind a login wall, for example. Disable it per-route with definePageMeta:

// pages/dashboard.vue
<script setup lang="ts">
definePageMeta({
  ssr: false  // Renders only on the client
})
</script>

Auto-imports in Practice

Anything placed under composables/ or components/ becomes globally available. Create a composable to fetch user data:

// composables/useUser.ts
export const useUser = () => {
  const user = ref<{ name: string; email: string } | null>(null)

  const fetchUser = async (id: number) => {
    user.value = await $fetch(`/api/users/${id}`)
  }

  return { user, fetchUser }
}

Use it in any page — no import statement required:

// pages/profile.vue
<script setup lang="ts">
const { user, fetchUser } = useUser()  // No import needed

await fetchUser(1)
</script>

<template>
  <div v-if="user">
    <h1>{{ user.name }}</h1>
    <p>{{ user.email }}</p>
  </div>
</template>

All Vue 3 built-ins work the same way — ref, computed, watch, onMounted, none of them need an import inside a Nuxt project. That one change alone cuts a surprising amount of repetitive boilerplate from every component file.

Writing API Routes with Nitro

Files inside server/api/ become HTTP endpoints automatically. Here is a minimal user lookup:

// server/api/users/[id].get.ts
export default defineEventHandler(async (event) => {
  const id = getRouterParam(event, 'id')

  // Replace this with a real database call
  const users: Record<string, { name: string; email: string }> = {
    '1': { name: 'Alice', email: '[email protected]' },
    '2': { name: 'Bob', email: '[email protected]' }
  }

  const user = users[id as string]

  if (!user) {
    throw createError({ statusCode: 404, message: 'User not found' })
  }

  return user
})

The filename [id].get.ts does two things at once: [id] creates a dynamic route parameter, and .get restricts the endpoint to GET requests only. Want a POST endpoint? Name the file .post.ts. Nitro handles method routing without any extra configuration.

For a real database, SQLite works well during development. Install the driver:

npm install better-sqlite3
npm install -D @types/better-sqlite3
// server/utils/db.ts
import Database from 'better-sqlite3'

const db = new Database('./dev.db')

// Auto-imported in all server/ files
export { db }

Environment Variables and Runtime Config

Secrets never belong in source code. Nuxt reads .env files and maps variables through runtimeConfig:

# .env
NUXT_SECRET_KEY=my-super-secret
NUXT_PUBLIC_API_BASE=https://api.example.com
// nuxt.config.ts
export default defineNuxtConfig({
  runtimeConfig: {
    secretKey: '',       // Server-only (overridden by NUXT_SECRET_KEY)
    public: {
      apiBase: ''        // Client + server (overridden by NUXT_PUBLIC_API_BASE)
    }
  }
})

The naming convention matters: NUXT_SECRET_KEY maps to runtimeConfig.secretKey, and NUXT_PUBLIC_API_BASE maps to runtimeConfig.public.apiBase. Nuxt handles the mapping automatically based on the prefix — no manual parsing needed. Access them in server routes:

// server/api/protected.get.ts
export default defineEventHandler((event) => {
  const config = useRuntimeConfig(event)
  // config.secretKey is available here, never exposed to the client
  return { status: 'ok' }
})

Verification & Monitoring

Running the Development Server

Start the dev server and open http://localhost:3000 in your browser:

npm run dev

Nuxt logs which routes it detected and confirms Nitro is running. A healthy startup looks like this:

  ➜  Local:    http://localhost:3000/
  ➜  Network:  http://192.168.x.x:3000/
  ✓  Nitro built in 300ms

Building for Production

The production build compiles everything into a single .output/ directory. Nitro bundles the server and static assets together:

npm run build

Once the build finishes, start the server:

node .output/server/index.mjs

No Vite, no dev dependencies in the runtime path. Just compiled output running as a plain Node.js process.

Checking SSR Is Actually Working

Open DevTools, go to the Network tab, and reload the page. Click the main HTML document response. With SSR active, the response body contains full rendered HTML — not a bare <div id="app"></div> waiting for JavaScript to fill it in.

Faster check from the terminal:

curl http://localhost:3000/ | grep '<h1'

If the heading text shows up in curl output, the page is server-rendered. Get nothing back and SSR is not firing for that route.

Monitoring with Nuxt DevTools

Nuxt 4 ships with an embedded DevTools panel. Enable it in nuxt.config.ts:

export default defineNuxtConfig({
  devtools: { enabled: true }
})

Restart the dev server and visit http://localhost:3000. A small Nuxt icon appears at the bottom of the page — click it to open the panel. From there you can inspect:

  • All detected pages and their route parameters
  • Auto-imported composables and components
  • Server API routes registered by Nitro
  • Runtime config values (public only)

Basic Health Check Endpoint

Give your deployment platform something to ping. A minimal health check:

// server/api/health.get.ts
export default defineEventHandler(() => {
  return {
    status: 'ok',
    timestamp: new Date().toISOString()
  }
})
curl http://localhost:3000/api/health
# {"status":"ok","timestamp":"2024-..."}

Point an uptime monitor — UptimeRobot, Betterstack, or whatever your team already uses — at this endpoint. You will get an alert within a minute of the server going down rather than finding out from a user report.

Where to Go from Here

At this point you have SSR for fast initial loads, auto-imports keeping code clean, and Nitro handling both rendering and API routes from a single deployment. Three solid foundations to build on.

Reasonable next steps: wire up Drizzle ORM for type-safe database access (it integrates cleanly with Nitro’s server utilities), add authentication with nuxt-auth-utils, and deploy to Railway, Render, or a VPS running PM2. All three are well-documented and require no major rework of what you have built here.

This stack has been running in production with a PostgreSQL backend across two sites — 50k+ monthly pageviews combined — without hitting any architectural limits. The conventions feel restrictive at first. Once you have shipped something real with them, they start to feel like guard rails rather than fences.

Share: