部署到 Fly.io
本指南介绍如何将使用 Prisma ORM 和 PostgreSQL 的 Node.js 服务器部署到 Fly.io。
¥This guide explains how to deploy a Node.js server that uses Prisma ORM and PostgreSQL to Fly.io.
Prisma Render 部署示例 包含一个带有 REST 端点和简单前端的 Express.js 应用。此应用使用 Prisma Client 从其数据库中获取、创建和删除记录。本指南将向你展示如何在 Fly.io 上部署相同的应用(无需修改)。
¥The Prisma Render deployment example contains an Express.js application with REST endpoints and a simple frontend. This app uses Prisma Client to fetch, create, and delete records from its database. This guide will show you how to deploy the same application, without modification, on Fly.io.
关于 Fly.io
¥About Fly.io
fly.io 是一个云应用平台,可让开发者轻松部署和扩展在靠近用户的机器上根据请求启动的全栈应用。对于此示例,了解以下内容很有帮助:
¥fly.io is a cloud application platform that lets developers easily deploy and scale full-stack applications that start on request near on machines near to users. For this example, it's helpful to know:
-
Fly.io 允许你在 全球 35 个地区 中部署长期运行的 "serverful" 全栈应用。默认情况下,应用在不使用时配置为 auto-stop,并在请求进入时根据需要自动启动。
¥Fly.io lets you deploy long-running, "serverful" full-stack applications in 35 regions around the world. By default, applications are configured to to auto-stop when not in use, and auto-start as needed as requests come in.
-
Fly.io 原生支持各种 语言和框架,包括 Node.js 和 Bun。在本指南中,我们将使用 Node.js 运行时。
¥Fly.io natively supports a wide variety of languages and frameworks, including Node.js and Bun. In this guide, we'll use the Node.js runtime.
-
Fly.io 可以 直接从 GitHub 启动应用。从 CLI 运行时,
fly launch
将自动配置托管在 GitHub 上的应用以在推送时部署。¥Fly.io can launch apps directly from GitHub. When run from the CLI,
fly launch
will automatically configure applications hosted on GitHub to deploy on push.
先决条件
¥Prerequisites
获取示例代码
¥Get the example code
将 示例代码 下载到本地机器。
¥Download the example code to your local machine.
curl https://codeload.github.com/prisma/prisma-examples/tar.gz/latest | tar -xz --strip=2 prisma-examples-latest/deployment-platforms/render
cd render
了解示例
¥Understand the example
在部署应用之前,让我们看一下示例代码。
¥Before we deploy the app, let's take a look at the example code.
Web 应用
¥Web application
Express 应用的逻辑位于两个文件中:
¥The logic for the Express app is in two files:
-
src/index.js
:API。端点使用 Prisma Client 从数据库中获取、创建和删除数据。¥
src/index.js
: The API. The endpoints use Prisma Client to fetch, create, and delete data from the database. -
public/index.html
:Web 前端。前端调用一些 API 端点。¥
public/index.html
: The web frontend. The frontend calls a few of the API endpoints.
Prisma 模式和迁移
¥Prisma schema and migrations
此应用的 Prisma 组件位于三个文件中:
¥The Prisma components of this app are in three files:
-
prisma/schema.prisma
:此应用的数据模型。此示例定义了两个模型,User
和Post
。此文件的格式遵循 Prisma 架构。¥
prisma/schema.prisma
: The data model of this app. This example defines two models,User
andPost
. The format of this file follows the Prisma schema. -
prisma/migrations/<migration name>/migration.sql
:在 PostgreSQL 数据库中构建此模式的 SQL 命令。你可以通过运行prisma migrate dev
自动生成像这样的迁移文件。¥
prisma/migrations/<migration name>/migration.sql
: The SQL commands that construct this schema in a PostgreSQL database. You can auto-generate migration files like this one by runningprisma migrate dev
. -
prisma/seed.js
:定义一些测试用户和帖子 Prisma,用于 播种数据库 和初始数据。¥
prisma/seed.js
: defines some test users and postsPrisma, used to seed the database with starter data.
部署示例
¥Deploy the example
1. 运行 fly launch
并接受默认值
¥ Run fly launch
and accept the defaults
就是这样。部署完成后,你的 Web 服务将在其 fly.dev
URL 上线。根据需要可选地 scale 机器的大小、数量和位置。fly console
可用于通过 ssh 连接到新机器或现有机器。
¥That’s it. Your web service will be live at its fly.dev
URL as soon as the deploy completes. Optionally scale the size, number, and placement of machines as desired. fly console
can be used to ssh into a new or existing machine.
可以在 fly.io 文档 中找到更多信息。
¥More information can be found on in the fly.io documentation.