# 5 分钟上手

## 1. 安装并验证

<Tabs items={['macOS / Linux', 'Windows', 'Docker']}>
  <Tab value="macOS / Linux">

```bash
curl -fsSL https://deno.land/install.sh | sh
deno --version
```

  </Tab>
  <Tab value="Windows">

```powershell
irm https://deno.land/install.ps1 | iex
deno --version
```

  </Tab>
  <Tab value="Docker">

```bash
docker run --rm denoland/deno:latest deno --version
```

  </Tab>
</Tabs>

## 2. 初始化并运行

```bash
deno init --serve hello-deno
cd hello-deno
deno task dev
```

`deno init --serve` 会创建 `deno.json`、服务入口和测试。模板服务需要监听网络；如果你手写入口，可明确运行：

```ts title="main.ts"
Deno.serve((_request) => Response.json({ ok: true }));
```

```bash
deno run --allow-net main.ts
curl http://localhost:8000
# {"ok":true}
```

## 3. 添加测试

```ts title="math_test.ts"
import { assertEquals } from "jsr:@std/assert";

Deno.test("adds two numbers", () => {
  assertEquals(2 + 3, 5);
});
```

```bash
deno test
```

## 4. 提交前门禁

```bash
deno fmt --check
deno lint
deno check **/*.ts
deno test
```

<Callout type="warn" title="不要先用 -A 解决权限错误">
`-A` 等于 `--allow-all`，会关闭权限沙箱。服务只需要监听端口时，使用 `--allow-net`；需要读取特定密钥时，使用 `--allow-env=KEY_NAME`。
</Callout>

官方依据：[Get started](https://docs.deno.com/runtime/)、[Installation](https://docs.deno.com/runtime/getting_started/installation/)、[Testing](https://docs.deno.com/runtime/test/)。
