Scripts2026年4月19日·1 分钟阅读

Nitro — Universal Server Toolkit for JavaScript

Nitro is a portable server framework from the UnJS ecosystem that builds and deploys to Node.js, Deno, Bun, Cloudflare Workers, and 15+ other runtimes from a single codebase.

Introduction

Nitro is a server toolkit from the UnJS ecosystem (the team behind Nuxt). It provides a universal server layer that runs on any JavaScript runtime and deploys to any hosting platform. Nitro powers Nuxt 3's server engine and is also used standalone by TanStack Start, Analog, and other frameworks that need a portable server backend.

What Nitro Does

  • Builds server applications that deploy to Node.js, Deno, Bun, Cloudflare Workers, Vercel, and more
  • Provides file-based routing for API endpoints and server middleware
  • Auto-imports utilities and handles TypeScript compilation without configuration
  • Generates optimized deployment bundles for each target platform
  • Includes built-in key-value storage, caching, and task scheduling

Architecture Overview

Nitro uses Rollup and esbuild under the hood to bundle server code into a self-contained output. At build time, it resolves the target preset (e.g., cloudflare, node-server, deno-deploy) and generates the appropriate entry point and platform-specific wrappers. The runtime uses h3 as its HTTP framework, which provides a minimal, high-performance event handler system. Routes defined in the routes/ directory are automatically registered, and middleware runs in order before handlers.

Self-Hosting & Configuration

  • Initialize with npx giget nitro or add as a dependency to any project
  • Configuration lives in nitro.config.ts with typed options
  • Set the deployment target via the preset option (e.g., node-server, cloudflare-pages)
  • Environment variables are accessed via useRuntimeConfig() with type-safe defaults
  • Build with npx nitropack build and run the output with node .output/server/index.mjs

Key Features

  • Universal deployment to 15+ platforms from one codebase with zero platform-specific code
  • Hot module replacement for server routes during development
  • Built-in useStorage() API with drivers for Redis, filesystem, Cloudflare KV, and more
  • Automatic OpenAPI schema generation from route handlers
  • WebSocket support with cross-platform compatibility

Comparison with Similar Tools

  • Express — Node.js-only with manual middleware setup; Nitro is universal with auto-imports
  • Hono — multi-runtime framework; Nitro adds file-based routing and build-time optimization
  • Fastify — Node.js focused with plugin system; Nitro targets portable deployment across runtimes
  • Cloudflare Workers — platform-specific; Nitro abstracts the runtime so you can switch targets
  • tRPC — type-safe API layer; Nitro is a full server framework that can host tRPC endpoints

FAQ

Q: Is Nitro only for Nuxt projects? A: No. Nitro is a standalone framework. Nuxt uses it as its server engine, but Nitro works independently and powers other frameworks like TanStack Start and Analog.

Q: Can I use Nitro with React or other frontend frameworks? A: Yes. Nitro is framework-agnostic on the server side. You can serve any frontend or use it purely as an API server.

Q: How does Nitro handle different deployment targets? A: Nitro uses presets that configure the build output for each platform. Switching from Node.js to Cloudflare Workers is a single config change.

Q: Does Nitro support server-sent events and streaming? A: Yes. Nitro supports SSE, streaming responses, and WebSockets across compatible runtimes.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产