patterntypescriptTip
Nitro Server Engine: Universal server with built-in auto-imports and storage
Viewed 0 times
nitroh3nuxtfile-based-routinguniversal-serverdeployment-targets
Problem
Building server APIs that work across multiple deployment targets (Node, serverless, edge) requires maintaining separate configurations and adapters for each environment, increasing complexity.
Solution
Use Nitro (the server engine behind Nuxt) for file-based routing, auto-imports, and built-in multi-target deployment. Define API routes in /server/api files and Nitro handles the rest — including tree-shaking, bundling, and adapter selection.
Why
Nitro abstracts deployment targets behind a unified API. One codebase deploys to Node, Cloudflare Workers, Vercel, Netlify, or AWS Lambda with a flag change. Its storage layer provides a unified KV interface across providers.
Gotchas
- Nitro's auto-import can obscure where functions come from — use explicit imports in shared libraries to maintain clarity.
- The h3 event object (used inside Nitro routes) has different ergonomics from Express req/res.
- Nitro's file-based routing uses bracket syntax for params: /server/api/users/[id].ts
Code Snippets
Nitro API route with typed event handler
// server/api/users/[id].get.ts
import { defineEventHandler, getRouterParam, createError } from 'h3'
export default defineEventHandler(async (event) => {
const id = getRouterParam(event, 'id')
if (!id) throw createError({ statusCode: 400, message: 'Missing id' })
const user = await useStorage('db').getItem(`users:${id}`)
if (!user) throw createError({ statusCode: 404, message: 'User not found' })
return user
})Revisions (0)
No revisions yet.