Web frameworks · Intermediate · 24 min
Build a server-rendered site with Nuxt 4
By Padding · Updated 18 Nov 2026
Node.js 26 · Nuxt 4.6.4 · Vue 3.5.45
You will build a server-rendered events listing with Nuxt 4.6, with file-based pages, a shared layout, data loaded through useFetch and useAsyncData, a JSON API in server/api, page titles from useSeoMeta, and a production build running as a plain Node.js server.
On this page
Nuxt is a framework built on Vue. It renders each page on the server, sends finished HTML, then loads Vue in the browser to make the page interactive. In the same project it runs Nitro, a small server engine that serves your pages and any API routes you write, and builds everything into one Node.js server for production.
You will build a listing of developer meetups: a home page that lists events and filters them by city, a page per event, an about page, a shared header and footer, a custom error page, and a JSON API the pages read from. Server rendering means the list is in the HTML the server sends, so it shows on a low-end Android phone on prepaid data before any JavaScript has downloaded, and search engines read it without running scripts.
The tutorial assumes Node.js 26 from the Node.js install tutorial and works the same on Linux and macOS.
Step 1 · Create the project
create-nuxt downloads an official starter and writes it to a new folder. The minimal template is the smallest one, --no-gitInit leaves Git for you to set up later, --no-modules skips the question about browsing and installing extra Nuxt modules, and --no-install lets you run the install yourself so you can see it. The starter's package.json allows newer Nuxt and Vue releases, so the second install pins the exact versions this tutorial was checked with:
cd ~
npx -y create-nuxt@4.0.3 nuxt-events-demo --template minimal --packageManager npm --no-gitInit --no-modules --no-install
cd nuxt-events-demo
npm install
npm install --save-exact nuxt@4.6.4 vue@3.5.45The install ends by running nuxt prepare, which generates TypeScript types into a hidden .nuxt/ folder. Nuxt may ask whether to share anonymous usage data the first time it runs; either answer is fine.
Check it worked
List the files and the installed versions:
ls app
cat nuxt.config.ts
npm ls nuxt vue --depth=0app/ holds one file, app.vue. Nuxt 4 keeps everything that runs in the browser (pages, layouts, components, composables) under app/, and server code under server/ next to it, so the two never mix. nuxt.config.ts sets a compatibilityDate and turns on the Nuxt DevTools. npm ls shows nuxt@4.6.4 and vue@3.5.45.
Step 2 · Run the development server
Start the dev server in your second terminal, from the nuxt-events-demo folder, and leave it running:
npm run devIt prints a local address, http://localhost:3000/. In your first terminal, ask for the home page:
curl -s http://localhost:3000/ | grep -o '<div id="__nuxt">'Check it worked
The output is <div id="__nuxt">, the element Vue takes over in the browser. Open http://localhost:3000/ in a browser and you see the Nuxt welcome page. View the page source: the welcome page's text is already in the HTML. A plain Vue app built with Vite sends an empty element and draws the page in the browser. Nuxt sends the finished markup and then hydrates it, which means Vue attaches its event handlers to the HTML that is already there.
Stop the dev server with Ctrl+C. The next steps add a server folder and a pages folder, and Nuxt reads both most reliably on a fresh start.
Step 3 · Write an events API with Nitro
Files in server/api/ become API routes under /api. A suffix such as .get.ts limits the route to one HTTP method, and a name in square brackets is a URL parameter. Files in server/utils/ are imported automatically into every server file, which makes them a good home for data access.
Start with the data. A list in memory is enough to learn with; later you can swap the body of these two functions for a database query without touching any route:
mkdir -p server/utils server/api/events
cat > server/utils/events.ts <<'TS'
export type TechEvent = {
id: string;
title: string;
city: string;
date: string;
venue: string;
summary: string;
};
const events: TechEvent[] = [
{
id: "lagos-frontend-meetup",
title: "Lagos Frontend Meetup",
city: "Lagos",
date: "2026-12-05",
venue: "Yaba Tech Hub",
summary: "Short talks on Vue, accessibility and building for low-end phones.",
},
{
id: "nairobi-api-night",
title: "Nairobi API Night",
city: "Nairobi",
date: "2026-12-10",
venue: "Kilimani Innovation Centre",
summary: "Designing APIs that stay fast on mobile data.",
},
{
id: "accra-vue-workshop",
title: "Accra Vue Workshop",
city: "Accra",
date: "2027-01-16",
venue: "Osu Community Library",
summary: "A hands-on morning building pages with Nuxt and Vue.",
},
];
export function listEvents(city?: string) {
const sorted = [...events].sort((a, b) => a.date.localeCompare(b.date));
if (!city) return sorted;
return sorted.filter((event) => event.city.toLowerCase() === city.toLowerCase());
}
export function findEvent(id: string) {
return events.find((event) => event.id === id);
}
TSNow the two routes. index.get.ts answers GET /api/events and reads an optional ?city= filter with getQuery. [id].get.ts answers GET /api/events/<id> and reads the ID with getRouterParam. When the event does not exist it throws createError, and Nitro turns that into a JSON error with a 404 status. defineEventHandler, getQuery and the rest are auto-imported, so the files need no import lines:
cat > server/api/events/index.get.ts <<'TS'
export default defineEventHandler((event) => {
const { city } = getQuery(event);
return listEvents(typeof city === "string" ? city : undefined);
});
TS
cat > 'server/api/events/[id].get.ts' <<'TS'
export default defineEventHandler((event) => {
const id = getRouterParam(event, "id") ?? "";
const found = findEvent(id);
if (!found) {
throw createError({ statusCode: 404, statusMessage: "Event not found" });
}
return found;
});
TSThe single quotes around 'server/api/events/[id].get.ts' stop the shell treating the square brackets as a pattern.
Start npm run dev again in the second terminal.
Check it worked
curl -s http://localhost:3000/api/events | grep -o '"id": *"[^"]*"'
curl -s 'http://localhost:3000/api/events?city=nairobi' | grep -o '"title": *"[^"]*"'
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/api/events/no-such-eventThe first command prints the three event IDs in date order, Lagos first. The second prints only the title line for Nairobi API Night, because the filter ignores letter case. The third prints 404. A handler that returns an object or array is sent as JSON with the right Content-Type; you do not call JSON.stringify yourself.
Step 4 · Add a layout, an about page and page titles
Three things turn the welcome page into a site:
app/layouts/default.vueis the frame around every page. Its<slot />is where the page goes.app/app.vuestops showing the welcome screen and renders<NuxtLayout>around<NuxtPage />, which shows whichever page matches the URL.- Files in
app/pages/are routes.about.vuebecomes/about.
<NuxtLink> renders a normal <a> tag. Once the app has hydrated, it moves between pages without a full reload, and it fetches the code for linked pages ahead of time.
Stop the dev server with Ctrl+C before you add the pages folder, then write the files:
mkdir -p app/layouts app/pages/events
cat > app/app.vue <<'VUE'
<template>
<NuxtRouteAnnouncer />
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
VUE
cat > app/layouts/default.vue <<'VUE'
<template>
<div class="site">
<header>
<NuxtLink to="/" class="brand">Tech Events</NuxtLink>
<nav aria-label="Main">
<NuxtLink to="/">All events</NuxtLink>
<NuxtLink to="/about">About</NuxtLink>
</nav>
</header>
<main>
<slot />
</main>
<footer>Listings are rendered on the server by Nuxt.</footer>
</div>
</template>
<style>
body { margin: 0; font-family: system-ui, sans-serif; line-height: 1.5; }
.site { max-width: 40rem; margin: 0 auto; padding: 1rem; }
header { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
header nav { display: flex; gap: 1rem; }
.brand { font-weight: 700; }
.router-link-exact-active { font-weight: 700; }
footer { margin-top: 2rem; font-size: 0.875rem; }
</style>
VUE
cat > app/pages/about.vue <<'VUE'
<script setup lang="ts">
useSeoMeta({
title: "About",
description: "Who runs this listing and how to add an event.",
});
</script>
<template>
<h1>About</h1>
<p>A small listing of developer meetups. Email the organisers to add yours.</p>
</template>
VUE<NuxtRouteAnnouncer /> tells screen readers the new page title after each client-side navigation, which a full page load would have done on its own. Keep it.
useSeoMeta sets the page's <title> and meta tags, on the server for the first load and in the browser after a navigation. Each key is typed, so a typo such as ogTitel is a type error, not a silent missing tag. Give every title the site name with a title template in nuxt.config.ts, and set the page language while you are there. This replaces the whole file:
cat > nuxt.config.ts <<'TS'
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
compatibilityDate: "2025-07-15",
devtools: { enabled: true },
app: {
head: {
htmlAttrs: { lang: "en" },
titleTemplate: "%s · Tech Events",
},
},
});
TSIf the site is in French, Swahili or Hausa, set lang to fr, sw or ha, so screen readers use the right voice and browsers offer the right translation.
Start npm run dev again.
Check it worked
curl -s http://localhost:3000/about | grep -o '<title>[^<]*</title>\|<h1>About</h1>\|Listings are rendered on the server'
curl -s http://localhost:3000/about | grep -o '<html[^>]*>'The first command prints <title>About · Tech Events</title>, <h1>About</h1> and the footer text, so the page rendered inside the layout. The second shows lang="en" on the <html> tag. In the browser, /about shows the header with the About link in bold.
Step 5 · List events with useFetch
useFetch loads data while the page renders. On the first request it runs on the server: Nuxt calls your API route directly, without a network round trip, and puts the result into the HTML. It also writes the data into the page as a JSON payload, so the browser does not fetch it a second time during hydration.
The query option takes a reactive value. Here it is the city from the URL, so when a filter link changes ?city=, useFetch asks the API again on its own. The title passed to useSeoMeta is a function for the same reason: it is re-read whenever the city changes.
cat > app/pages/index.vue <<'VUE'
<script setup lang="ts">
const route = useRoute();
const city = computed(() => (typeof route.query.city === "string" ? route.query.city : ""));
const { data: events, status } = await useFetch("/api/events", {
query: { city },
});
useSeoMeta({
title: () => (city.value ? `Events in ${city.value}` : "Upcoming events"),
description: "Developer meetups and workshops, listed by date.",
ogTitle: "Upcoming developer events",
});
</script>
<template>
<h1>{{ city ? `Events in ${city}` : "Upcoming events" }}</h1>
<p>
Filter:
<NuxtLink to="/">All</NuxtLink> ·
<NuxtLink :to="{ query: { city: 'Lagos' } }">Lagos</NuxtLink> ·
<NuxtLink :to="{ query: { city: 'Nairobi' } }">Nairobi</NuxtLink> ·
<NuxtLink :to="{ query: { city: 'Accra' } }">Accra</NuxtLink>
</p>
<p v-if="status === 'pending'">Loading events...</p>
<ul v-else class="events">
<li v-for="item in events" :key="item.id">
<NuxtLink :to="`/events/${item.id}`">{{ item.title }}</NuxtLink>
<span> · {{ item.city }} · {{ item.date }}</span>
</li>
</ul>
</template>
VUEuseFetch knows the return type of your API route, so item.title and item.date are typed from TechEvent without you writing a type on the page.
Everything a data call returns is copied into the page payload. With three events that is nothing, but with hundreds it adds up for readers on metered data. Return only the fields a page shows, from the API or with the transform option of useFetch.
Check it worked
curl -s http://localhost:3000/ | grep -o '<title>[^<]*</title>\|Lagos Frontend Meetup\|Nairobi API Night\|Accra Vue Workshop'
curl -s 'http://localhost:3000/?city=Accra' | grep -o '<title>[^<]*</title>\|Lagos Frontend Meetup\|Accra Vue Workshop'The first command prints <title>Upcoming events · Tech Events</title> and all three event titles, straight from the server's HTML. The second prints <title>Events in Accra · Tech Events</title> and only Accra Vue Workshop. In the browser, click Nairobi: the list changes without a full page load, and the browser's network panel shows one request to /api/events?city=Nairobi.
Step 6 · Show one event with useAsyncData
useFetch(url) is a shorthand for useAsyncData plus $fetch(url). Use useAsyncData directly when the data does not come from a single URL, for example when you call two APIs or read from an SDK. It takes a key, which Nuxt uses to match the data in the payload to the call in the browser, so make the key unique per event.
app/pages/events/[id].vue matches /events/<anything>, and the part in brackets arrives as route.params.id. When the API answers 404, the page throws createError with fatal: true. On the server that sends a real 404 status; in the browser it shows the error page:
cat > 'app/pages/events/[id].vue' <<'VUE'
<script setup lang="ts">
const route = useRoute();
const id = String(route.params.id);
const { data: event, error } = await useAsyncData(`event-${id}`, () =>
$fetch(`/api/events/${id}`),
);
if (error.value || !event.value) {
throw createError({ statusCode: 404, statusMessage: "Event not found", fatal: true });
}
useSeoMeta({
title: event.value.title,
description: event.value.summary,
ogTitle: event.value.title,
ogDescription: event.value.summary,
});
</script>
<template>
<article v-if="event">
<h1>{{ event.title }}</h1>
<p>{{ event.date }} · {{ event.venue }}, {{ event.city }}</p>
<p>{{ event.summary }}</p>
<NuxtLink to="/">Back to all events</NuxtLink>
</article>
</template>
VUEThe error page is app/error.vue. It sits outside the pages, so it wraps itself in <NuxtLayout> to keep the header. clearError resets the error and moves the reader to a safe page:
cat > app/error.vue <<'VUE'
<script setup lang="ts">
import type { NuxtError } from "#app";
const props = defineProps<{ error: NuxtError }>();
useSeoMeta({ title: props.error.statusCode === 404 ? "Page not found" : "Something went wrong" });
</script>
<template>
<NuxtLayout>
<h1 v-if="error.statusCode === 404">We could not find that page</h1>
<h1 v-else>Something went wrong</h1>
<p>{{ error.statusMessage }}</p>
<button type="button" @click="clearError({ redirect: '/' })">Back to all events</button>
</NuxtLayout>
</template>
VUECheck it worked
curl -s http://localhost:3000/events/nairobi-api-night | grep -o '<title>[^<]*</title>\|<h1>[^<]*</h1>'
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/events/no-such-event
curl -s -H 'Accept: text/html' http://localhost:3000/events/no-such-event | grep -o 'We could not find that page'The first command prints <title>Nairobi API Night · Tech Events</title> and <h1>Nairobi API Night</h1>. The second prints 404, and the third shows the custom error heading. The third command sends Accept: text/html the way a browser does; without it, Nuxt answers an error with JSON instead of the error page. A real 404 status matters: search engines drop the URL instead of indexing an empty page.
Step 7 · Build for Node.js and run the output
Stop the dev server with Ctrl+C.
Nitro builds for many hosts through presets. node-server is the default for nuxt build; naming it makes the target clear in scripts and CI:
NITRO_PRESET=node-server npm run build
ls .output .output/serverThe build ends with a line suggesting node .output/server/index.mjs. .output/public/ holds the browser files and .output/server/ holds the server, with the parts of node_modules it needs copied in. The .output folder runs on its own: to deploy, copy just that folder to a machine with Node.js 26. You do not run npm install there.
Start it in the second terminal. PORT sets the port (the default is 3000):
PORT=8080 node .output/server/index.mjsIt prints a line like Listening on http://[::]:8080.
Check it worked
curl -s http://localhost:8080/ | grep -o '<title>[^<]*</title>'
curl -s 'http://localhost:8080/api/events?city=lagos' | grep -o '"title": *"[^"]*"'
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8080/events/no-such-eventThe first command prints <title>Upcoming events · Tech Events</title>, the second the title line for Lagos Frontend Meetup, and the third 404: pages and API are served by the same production process. In the browser, open http://localhost:8080/, switch JavaScript off in the developer tools settings and reload. The list, the filter links and the event pages all still work, each as a full page load. To put this server behind Nginx with HTTPS, follow the Nginx reverse proxy tutorial, using port 8080.
Step 8 · Clean up
Stop the production server with Ctrl+C, then remove the project:
cd ~
rm -rf ~/nuxt-events-demoCheck it worked
ls ~/nuxt-events-demo reports No such file or directory, and curl http://localhost:8080/ fails to connect. Node.js stays installed.
Next steps
- Go back to the Vue basics that every Nuxt page uses: build your first Vue 3 app with the Composition API.
- Compare the React approaches to the same job: build a Next.js 16 App Router site and build a server-rendered app with React Router framework mode.
- Serve the production build on a domain with HTTPS: put Nginx in front of a Node.js app.
- The Nuxt documentation at nuxt.com covers route rules for caching or prerendering single pages, server middleware, runtime config for secrets and other deployment presets.
Part of these paths
Related tutorials
- Build your first Vue 3.5 app with the Composition APIYou will scaffold a Vue 3.5 and TypeScript project with create-vue, build a reading-list app with ref, computed, v-model, v-for, v-if, props and emitted events, then build it for production and check the result with vite preview.
- How to Build Your First Next.js 16 App with the App RouterYou will create a Next.js 16 app, add pages, a shared layout, a dynamic route that is built ahead of time, one interactive component and a JSON route, then run a production build and read what Next.js decided to prerender.
- Build a server-rendered app with React Router framework modeYou will build a server-rendered notes app with React Router 8.5 framework mode, with file-based routes, server loaders and actions, a form that works before JavaScript loads, validation errors and page titles, and run its production build with react-router-serve on Node.js.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials