# Turso + Railway

> 使用 [Turso Cloud 嵌入式副本](/features/embedded-replicas) 將 JavaScript/Rust 應用程式部署到 [Railway](https://railway.app/)。
> Canonical HTML: https://tursodb.cn/zh-tw/docs/features/embedded-replicas/with-railway/
> Upstream Source (MIT): https://docs.turso.tech/features/embedded-replicas/with-railway
> Language: zh-TW

---
![Koyeb banner](/images/turso-docs/platforms/railway-banner.png)

## 先決條件

開始之前，請確認您已完成下列事項：

* [安裝 Turso CLI](/zh-tw/docs/cli/installation/)
* [註冊或登入 Turso Cloud](/zh-tw/docs/cli/authentication/#signup)
* [安裝 Railway CLI](https://docs.railway.app/guides/cli#installing-the-cli)

**1. 取得資料庫認證資訊**

您需要一個現有的資料庫才能繼續。如果您還沒有，請[建立一個](/zh-tw/docs/quickstart/)。

取得資料庫 URL：

```bash
turso db show --url <database-name>
```

取得資料庫驗證權杖：

```bash
turso db tokens create <database-name>
```

將認證資訊指派到 `.env` 內的環境變數。

```bash
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

您會希望將這些資訊儲存為環境變數。

</div></div>


**2. 取得應用程式程式碼**

從 GitHub 複製（fork）並在本地複製（clone）下列嵌入式副本專案：

<div class="doc-cards">
<a class="doc-card" href="https://github.com/tursodatabase/embedded-replicas-with-js" target="_blank" rel="noopener noreferrer"><div class="doc-card-title"><span>My Expenses Tracker - (Elysia + Bun)</span><span class="doc-card-arrow" aria-hidden="true">↗</span></div><p class="doc-card-desc">查看完整原始碼</p></a>
<a class="doc-card" href="https://github.com/tursodatabase/embedded-replicas-with-rust" target="_blank" rel="noopener noreferrer"><div class="doc-card-title"><span>Web Traffic API - (Rust)</span><span class="doc-card-arrow" aria-hidden="true">↗</span></div><p class="doc-card-desc">查看完整原始碼</p></a>
</div>


**3. 建立新的 Railway 專案**

執行下列命令來建立新的 Railway 專案。出現提示時，請提供專案名稱。

```bash
railway init
```

**4. 在 Railway 專案中新增服務**

在您的 Railway 專案上[建立新的空白服務](https://docs.railway.app/guides/services#creating-a-service)，作為應用程式的部署目標。

**5. 將應用程式連結至服務**

執行下列命令來列出並選擇要連結至應用程式的服務：

```bash
railway service
```

**6. 新增資料庫認證資訊**

在 Railway 儀表板上開啟該服務，並新增您的 Turso Cloud 資料庫認證資訊。

```bash
TURSO_DATABASE_URL=libsql://[db-name]-[github-username].turso.io
TURSO_AUTH_TOKEN=...
LOCAL_DB=file:local-db-name.db
```

**7. 部署**

執行下列命令來部署您的應用程式：

```bash
railway up
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">注意</div><div class="doc-callout-body">

請務必[將應用程式公開至網際網路](https://docs.railway.app/guides/public-networking)，使其可從公開網路存取。

</div></div>


<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">警告</div><div class="doc-callout-body">

如果您使用的是免費方案，您需要將 Railway 帳號連結至 GitHub，才能使用程式碼部署功能。

</div></div>
