encore-getting-started
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGetting Started with Encore.ts
Encore.ts 入门指南
Instructions
操作步骤
Install Encore CLI
安装 Encore CLI
bash
undefinedbash
undefinedmacOS
macOS
brew install encoredev/tap/encore
brew install encoredev/tap/encore
Linux/WSL
Linux/WSL
curl -L https://encore.dev/install.sh | bash
curl -L https://encore.dev/install.sh | bash
Windows (PowerShell)
Windows (PowerShell)
iwr https://encore.dev/install.ps1 | iex
undefinediwr https://encore.dev/install.ps1 | iex
undefinedCreate a New App
创建新应用
bash
undefinedbash
undefinedInteractive - choose from templates
交互式 - 从模板中选择
encore app create my-app
encore app create my-app
Or start with a blank app
或者从空白应用开始
encore app create my-app --example=ts/hello-world
undefinedencore app create my-app --example=ts/hello-world
undefinedProject Structure
项目结构
A minimal Encore.ts app:
my-app/
├── encore.app # App configuration
├── package.json # Dependencies
├── tsconfig.json # TypeScript config
├── encore.service.ts # Service definition
└── api.ts # API endpoints一个最简版 Encore.ts 应用结构:
my-app/
├── encore.app # 应用配置文件
├── package.json # 依赖文件
├── tsconfig.json # TypeScript 配置
├── encore.service.ts # 服务定义文件
└── api.ts # API 端点文件The encore.app File
encore.app 文件
cue
// encore.app
{
"id": "my-app"
}This file marks the root of your Encore app. The is your app's unique identifier.
idcue
// encore.app
{
"id": "my-app"
}该文件标记你的 Encore 应用的根目录。 是应用的唯一标识符。
idDefine a Service
定义服务
Create to define a service:
encore.service.tstypescript
// encore.service.ts
import { Service } from "encore.dev/service";
export default new Service("my-service");创建 文件来定义服务:
encore.service.tstypescript
// encore.service.ts
import { Service } from "encore.dev/service";
export default new Service("my-service");Create Your First API
创建你的第一个 API
typescript
// api.ts
import { api } from "encore.dev/api";
interface HelloResponse {
message: string;
}
export const hello = api(
{ method: "GET", path: "/hello", expose: true },
async (): Promise<HelloResponse> => {
return { message: "Hello, World!" };
}
);typescript
// api.ts
import { api } from "encore.dev/api";
interface HelloResponse {
message: string;
}
export const hello = api(
{ method: "GET", path: "/hello", expose: true },
async (): Promise<HelloResponse> => {
return { message: "Hello, World!" };
}
);Run Your App
运行你的应用
bash
undefinedbash
undefinedStart the development server
启动本地开发服务器
encore run
encore run
Your API is now available at http://localhost:4000
你的 API 现在可以通过 http://localhost:4000 访问
undefinedundefinedOpen the Local Dashboard
打开本地控制台
bash
undefinedbash
undefinedOpens the local development dashboard
打开本地开发控制台
encore run
encore run
Then visit http://localhost:9400
The dashboard shows:
- All your services and endpoints
- Request/response logs
- Database queries
- Traces and spans
控制台展示以下内容:
- 所有服务和端点
- 请求/响应日志
- 数据库查询记录
- 追踪信息和调用链Common CLI Commands
常用 CLI 命令
| Command | Description |
|---|---|
| Start the local development server |
| Run tests |
| Open a psql shell to a database |
| Generate API client code |
| Link to an existing Encore Cloud app |
| 命令 | 描述 |
|---|---|
| 启动本地开发服务器 |
| 运行测试 |
| 打开数据库的 psql 命令行界面 |
| 生成 API 客户端代码 |
| 关联到已有的 Encore Cloud 应用 |
Add a Database
添加数据库
typescript
// db.ts
import { SQLDatabase } from "encore.dev/storage/sqldb";
const db = new SQLDatabase("mydb", {
migrations: "./migrations",
});Create a migration:
sql
-- migrations/1_create_table.up.sql
CREATE TABLE items (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);typescript
// db.ts
import { SQLDatabase } from "encore.dev/storage/sqldb";
const db = new SQLDatabase("mydb", {
migrations: "./migrations",
});创建迁移文件:
sql
-- migrations/1_create_table.up.sql
CREATE TABLE items (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);Next Steps
下一步
- Add more endpoints (see skill)
encore-api - Add authentication (see skill)
encore-auth - Add infrastructure like Pub/Sub, cron jobs (see skill)
encore-infrastructure - Deploy to Encore Cloud: then
encore app linkgit push encore
- 添加更多端点(参考 技能文档)
encore-api - 添加认证功能(参考 技能文档)
encore-auth - 添加 Pub/Sub、定时任务等基础设施(参考 技能文档)
encore-infrastructure - 部署到 Encore Cloud:先执行 ,再执行
encore app linkgit push encore