File-based Routing: Create API endpoints by placing files in server/api/
. Each file exports a default defineEventHandler()
.
Browse / Nuxt 3 Server-Side Essentials
Nuxt 3 Server-Side Essentials
A concise guide to leveraging Nuxt 3's powerful server-side features, from API routes and middleware to database integration and robust deployment strategies using Nitro.
Nuxt 3 Server-Side Essentials
SERVER ROUTES & HANDLERS
|
Example:
Access at |
Define Endpoints with |
Accessing Low-Level Node.js APIs: The |
Example: Custom headers
|
Error Handling: Throw
|
Pro Tip: Use |
USEFUL UTILITIES
`getQuery(event)` |
Parses and returns query parameters from the request URL.
|
`readBody(event)` |
Parses and returns the request body. Supports JSON, URL-encoded, etc.
|
`sendRedirect(event, url, statusCode)` |
Redirects the client to a new URL with a specified HTTP status code (default 302).
|
`useRuntimeConfig()` |
Accesses environment variables and runtime configuration defined in
|
`getHeader(event, name)`
|
Retrieves a specific request header or all headers from the incoming request.
|
`setHeaders(event, headers)`
|
Sets or appends response headers for the outgoing response.
|
**Common Mistake:** |
Using client-side composables like |
MIDDLEWARE & AUTH
Defining Middleware: Place files in |
Example:
|
Authentication Guards: Use middleware to protect routes by checking authentication status. If unauthorized, throw an error or redirect. |
Example:
|
Using Composables in Middleware: While most client-side composables aren’t available, you can define your own server-side composables or use utilities like |
Data Sharing: Middleware can populate
|
Pro Tip: For specific route protection, consider using a named middleware inside |
DATABASE & INTEGRATION
Working with Databases: Nuxt 3’s Nitro server engine allows direct interaction with databases like PostgreSQL, MySQL, MongoDB, or ORMs like Prisma. |
Example (Prisma Integration):
|
Using Prisma Client in Server Handlers: Instantiate your Prisma client and use |
|
|
Environment Variables for DB Connections: Securely manage sensitive data like database URLs using |
Example
Access in Nuxt: |
Async/Await Best Practices: Always |
Common Mistake: Not handling database connection pooling or singleton patterns, leading to excessive connections and performance issues in production. Use a global singleton for your Prisma client or similar ORM. |
DEPLOYMENT & HOSTING
Running Nuxt in SSR Mode: By default, Nuxt 3 applications are server-side rendered, meaning HTML is generated on the server before being sent to the client. This improves SEO and initial load times. |
Nitro Engine: Nuxt 3 uses Nitro, a powerful server engine that abstracts away underlying server platforms, allowing for flexible deployments to various environments. |
Deployment Adapters: Nitro provides adapters to optimize your Nuxt app for different hosting platforms. Configure in |
Common Adapters:
|
Production Build ( |
Local Production Preview ( |
Deployment Flow (General):
|
Pro Tip: Always test your production build locally with |
Common Mistake: Forgetting to configure |