What is Nitro?
Nitro is a modern server engine that provides the server-side functionality for Nuxt applications. It was created while building Nuxt and offers powerful features that make Nuxt applications fast, flexible, and deployable anywhere.Key Features
Nitro ships with many powerful features:- Cross-platform support - Node.js, browsers, service workers, and more
- Serverless support - Out-of-the-box serverless compatibility
- API routes support - Easy creation of server API endpoints
- Automatic code-splitting - Async-loaded chunks for optimal performance
- Hybrid mode - Support for static + serverless sites
- Hot module reloading - Development server with HMR for rapid development
API Layer
Server API endpoints and middleware are powered by Nitro, which internally uses h3, a minimal HTTP framework. Key features of the API layer include:- Handlers can directly return objects/arrays for automatically-handled JSON responses
- Handlers can return promises, which will be awaited
- Helper functions for body parsing, cookie handling, redirects, headers, and more
Creating API Routes
You can create API routes in theserver/api/ directory:
server/api/test.ts
Direct API Calls
Nitro allows ‘direct’ calling of routes via the globally-available$fetch helper. This will make an API call to the server if run on the browser, but will directly call the relevant function if run on the server, saving an additional API call.
The $fetch API uses ofetch, with key features including:
- Automatic parsing of JSON responses (with access to raw response if needed)
- Request body and params are automatically handled with correct
Content-Typeheaders
Typed API Routes
When using API routes (or middleware), Nitro generates typings for these routes as long as you’re returning a value instead of usingres.end() to send a response.
You can access these types when using $fetch() or useFetch():
Standalone Server
Nitro produces a standalone server distribution that is independent ofnode_modules. This makes your server:
- Portable - The server can run in any environment
- Small - No need to ship your entire
node_modulesfolder - Fast - Optimized for production performance
nuxt build into a .output directory.
The output contains:
- Runtime code to run your Nuxt server in any environment
- Static files ready to be served
- Support for experimental browser service workers
- A native storage layer with multi-source drivers
Universal Deployment
Nitro offers the ability to deploy your Nuxt app anywhere, from a bare metal server to the edge network, with a start time of just a few milliseconds. There are 15+ presets to build your Nuxt app for different cloud providers and servers, including:- Cloudflare Workers
- Netlify Functions
- Vercel Cloud
- Deno
- Bun
- And many more
- Start with one provider and switch to another without code changes
- Deploy to multiple providers simultaneously
- Use the same codebase for different deployment targets
Hybrid Rendering
Nitro has a powerful feature calledrouteRules that allows you to define a set of rules to customize how each route of your Nuxt app is rendered.
nuxt.config.ts
ssr, appMiddleware, and noScripts) and change the behavior when rendering pages to HTML. Others also affect client-side behavior (appMiddleware, redirect, and prerender).
Performance Benefits
Nitro provides several performance optimizations:- Code splitting - Only load what’s needed
- Tree shaking - Remove unused code
- Minification - Reduce bundle size
- Caching - Built-in caching layer
- Compression - Automatic response compression
- Edge deployment - Render closer to users