快速开始
系统要求
开始之前,请确保你的系统满足以下要求:
- Node.js 18.18 或更高版本
- Bun 1.0 或更高版本(推荐)
- macOS、Windows(包括 WSL)或 Linux
自动安装
创建新的 HestJS 应用最快的方法是使用 create-hest-app
,它会自动为你设置所有内容。要创建项目,请运行:
npx create-hest-app@latest
安装时,你会看到以下提示:
✔ Would you like to use ESLint? › Yes
✔ Which template would you like to use? › Base - A simple HestJS application with basic features
✔ Would you like to include Swagger/Scalar API documentation? (adds ~12MB to build size) › No
✔ Which package manager would you like to use? › bun
✔ Skip installing dependencies? › No
在提示完成后,create-hest-app
将创建一个以你的项目名称命名的文件夹并安装所需的依赖项。
如果你选择跳过安装依赖项,你需要手动安装:
cd my-app
bun install
运行开发服务器
bun dev
你应该会看到类似以下的输出:
[INFO] 🚀 Starting HestJS application...
[INFO] 🔍 Mapped {/api, GET}
[INFO] ✅ Module UsersModule initialized
[INFO] ✅ Module CustomValidationModule initialized
[INFO] ✅ Module AppModule initialized
[INFO] 🔍 Mapped {/api/users, GET}
[INFO] 🆔 Mapped {/api/users/:id, GET}
[INFO] 📩 Mapped {/api/users, POST}
打开浏览器访问 http://localhost:3002/api 查看结果。
项目结构
成功创建后,你的项目结构应该如下所示:
my-app/
├── src/
│ ├── index.ts
│ ├── app.module.ts
│ ├── app.controller.ts
│ ├── app.service.ts
│ ├── modules/
│ └── common/
├── package.json
├── tsconfig.json
├── eslint.config.ts
└── README.md
编辑你的第一个页面
打开 src/app.controller.ts
并编辑 getHello
方法:
import { Controller, Get } from '@hestjs/core';
import { Context } from 'hono';
@Controller('/api')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('/')
getHello(c: Context) {
return c.json({
message: 'Hello from HestJS!',
timestamp: new Date().toISOString()
});
}
}
保存文件后,开发服务器会自动重新加载更改。