服务器
由 Nitro 提供支持
¥Powered by Nitro
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:
export default defineEventHandler(async (event) => {
// ... Do whatever you want here
})
你可以直接返回 text
、json
、html
甚至 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.
通用部署
¥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!
有超过 15 个预设可用于为不同的云提供商和服务器构建你的 Nuxt 应用,包括:
¥There are more than 15 presets to build your Nuxt app for different cloud providers and servers, including:
或者对于其他运行时:
¥Or for other runtimes:
混合渲染
¥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).
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 }
}
// ...
}
})
:
了解所有可用的路由规则,这些规则可用于自定义路由的渲染模式。
¥Learn about all available route rules are available to customize the rendering mode of your routes.
::
此外,还有一些特定于 Nuxt 的路由规则(例如 ssr
、appMiddleware
和 noScripts
),用于更改页面渲染为 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.
某些路由规则(appMiddleware
、redirect
和 prerender
)也会影响客户端行为。
¥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.