入门指南


¥Getting Started

创建具有所有必要功能的 Web 服务器,并将其部署到你喜欢的任何位置。

简介

¥Intro

Nitro 是一个开源框架,用于使用 unjs/h3 和许多内置功能构建 Web 服务器。Nitro 会自动使你的代码与任何 deployment 提供程序和运行时兼容!

¥Nitro is an open source framework to build web servers using unjs/h3 and lots of built-in features. Nitro automatically makes your code compatible with any deployment provider and runtime!

Nitro 可以独立使用,也可以作为 Nuxt 等全栈框架的服务器引擎使用。

快速入门

¥Quick start

你可以使用 在线运行,而无需设置本地开发环境。
确保你已安装推荐的设置:- Node.jsBunDeno 的最新 LTS 版本。 - Visual Studio Code

使用入门模板创建一个新项目:

¥Create a new project using starter template:

npx giget@latest nitro nitro-app --install
cd nitro-app

启动开发服务器:

¥Start the development server:

npm run dev

Nitro 已准备好支持 http://localhost:3000/

¥Nitro is ready at http://localhost:3000/!

如果你想知道发生了什么,请检查 .nitro/dev/index.mjs

构建你的生产就绪服务器:

¥Build your production-ready server:

npm run build

输出位于 .output 目录中,几乎可以部署到任何没有依赖的提供商上。

¥Output is in the .output directory and ready to be deployed on almost any provider with no dependencies.

你可以使用以下命令在本地尝试:

¥You can try it locally with:

npm run preview
你可以在 Nitro 代码库中找到更多示例:nitrojs/nitro/examples

目录结构

¥Directory structure

入门模板包含一些重要的文件,可帮助你入门。

¥The starter template includes some important files to get you started.

server/routes/

server/routes/ 目录包含你的应用处理程序。你可以在 server/routes/ 目录中创建子目录以创建嵌套处理程序。文件名即路由路径。

¥The server/routes/ directory contains your application handlers. You can create subdirectories inside server/routes/ dir to create nested handlers. The file name is the route path.

Read more in Guide > Routing.

server/api/

server/api/ 目录与 server/routes/ 类似,唯一的区别是,为了方便起见,其中的路由会以 /api/ 为前缀。

¥The server/api/ directory is similar to server/routes/ with the only difference that routes inside it will be prefixed with /api/ for convenience.

Read more in Guide > Routing.

server/utils/

此目录包含支持自动导入的应用工具。

¥This directory contains your application utils with auto import support.

Read more in Guide > Utils.

server/plugins/

此目录包含你的自定义 nitro 插件。

¥This directory contains your custom nitro plugins.

Read more in Guide > Plugins.

nitro.config.ts

nitro.config.ts 文件包含 Nitro 的配置。

¥The nitro.config.ts file contains the configuration for Nitro.

Read more in Guide > Configuration.

tsconfig.json

tsconfig.json 文件包含项目的 TypeScript 配置。

¥The tsconfig.json file contains the TypeScript configuration for your project.

Read more in Guide > Typescript.

package.json

package.json 文件包含项目的所有依赖和脚本。

¥The package.json file contains all the dependencies and scripts for your project.