服务器

使用 Nuxt 的服务器框架构建全栈应用。你可以从数据库或其他服务器获取数据、创建 API,甚至生成静态服务器端内容,例如站点地图或 RSS 源。 - 所有这些都来自一个代码库。
Read more in Docs > Guide > Directory Structure > Server.

由 Nitro 提供支持

¥Powered by Nitro

Server engine

Nuxt 的服务器是 Nitro。它最初是为 Nuxt 创建的,但现在是 UnJS 的一部分,并向其他框架开放。 - 甚至可以单独使用。

¥Nuxt's server is Nitro. It was originally created for Nuxt but is now part of UnJS and open for other frameworks - and can even be used on its own.

使用 Nitro 赋予 Nuxt 超能力:

¥Using Nitro gives Nuxt superpowers:

  • 函数将下一个路由 作为第一个参数,将当前路由 作为第二个参数,这两个参数都是 Vue 路由对象。
  • 可在任何提供商上通用部署(许多零配置)
  • 混合渲染

Nitro 内部使用 h3,这是一个为高性能和可移植性而构建的极简 H(TTP) 框架。

¥Nitro is internally using h3, a minimal H(TTP) framework built for high performance and portability.

服务器端点和中间件

¥Server Endpoints & Middleware

你可以轻松管理 Nuxt 应用的服务器端部分,从 API 端点到中间件。

¥You can easily manage the server-only part of your Nuxt app, from API endpoints to middleware.

端点和中间件可以这样定义:

¥Both endpoints and middleware can be defined like this:

server/api/test.ts
export default defineEventHandler(async (event) => {
  // ... Do whatever you want here
})

你可以直接返回 textjsonhtml 甚至 stream

¥And you can directly return text, json, html or even a stream.

开箱即用,它支持热模块替换和自动导入,就像 Nuxt 应用的其他部分一样。

¥Out-of-the-box, it supports hot module replacement and auto-import like the other parts of your Nuxt application.

Read more in Docs > Guide > Directory Structure > Server.

通用部署

¥Universal Deployment

Nitro 允许你将 Nuxt 应用部署到任何地方,从裸机服务器到边缘网络,启动时间仅为几毫秒。真快!

¥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. That's fast!

Read more in Blog > Nuxt On The Edge.

有超过 15 个预设可用于为不同的云提供商和服务器构建你的 Nuxt 应用,包括:

¥There are more than 15 presets to build your Nuxt app for different cloud providers and servers, including:

或者对于其他运行时:

¥Or for other runtimes:

Deno

Bun

Read more in Docs > Getting Started > Deployment.

混合渲染

¥Hybrid Rendering

Nitro 拥有一项名为 routeRules 的强大功能,它允许你定义一组规则来自定义 Nuxt 应用中每个路由的渲染方式(以及更多)。

¥Nitro has a powerful feature called routeRules which allows you to define a set of rules to customize how each route of your Nuxt app is rendered (and more).

nuxt.config.ts
export default defineNuxtConfig({
  routeRules: {
    // Generated at build time for SEO purpose
    '/': { prerender: true },
    // Cached for 1 hour
    '/api/*': { cache: { maxAge: 60 * 60 } },
    // Redirection to avoid 404
    '/old-page': {
      redirect: { to: '/new-page', statusCode: 302 }
    }
    // ...
  }
})

:

Read more in Docs > Guide > Concepts > Rendering#hybrid Rendering.

了解所有可用的路由规则,这些规则可用于自定义路由的渲染模式。

¥Learn about all available route rules are available to customize the rendering mode of your routes.

::

此外,还有一些特定于 Nuxt 的路由规则(例如 ssrappMiddlewarenoScripts),用于更改页面渲染为 HTML 时的行为。

¥In addition, there are some route rules (for example, ssr, appMiddleware, and noScripts) that are Nuxt specific to change the behavior when rendering your pages to HTML.

某些路由规则(appMiddlewareredirectprerender)也会影响客户端行为。

¥Some route rules (appMiddleware, redirect and prerender) also affect client-side behavior.

Nitro 用于构建应用的服务器端渲染和预渲染。

¥Nitro is used to build the app for server side rendering, as well as pre-rendering.

Read more in Docs > Guide > Concepts > Rendering.