-
Notifications
You must be signed in to change notification settings - Fork 3
フロントエンドとバックエンドの連携とデプロイの節を追加 #890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Deploying utcode-learn with
|
| Latest commit: |
eeaf1d7
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://a0f964e5.utcode-learn.pages.dev |
| Branch Preview URL: | https://add-deploy-chapter.utcode-learn.pages.dev |
89884e6 to
a54ea15
Compare
e0beb3e to
e53da51
Compare
docs/4-advanced/05-integration-and-deployment/_samples/fullstack-app/frontend/src/App.tsx
Show resolved
Hide resolved
e53da51 to
28be016
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a comprehensive new section on "Frontend and Backend Integration and Deployment" to the advanced documentation. The changes include term definitions for key concepts like tsx (TypeScript Execute), origins, and CORS, along with supporting visual materials and sample code for a fullstack application.
Key Changes
- Added three new technical term definitions (tsx, origin, CORS) with Japanese descriptions and reference links
- Included visual assets (PNG diagram and SVG flowchart) to illustrate web service architecture and data flow
- Provided a React component to visually explain the concept of origins
- Added frontend boilerplate configuration files (TypeScript configs, Vite setup, package.json)
Reviewed Changes
Copilot reviewed 22 out of 32 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/components/Term/type-map.js | Maps new terms (tsx, オリジン, CORS) to their definition keys |
| src/components/Term/definitions.js | Defines technical terms with Japanese descriptions and reference pages |
| docs/4-advanced/05-integration-and-deployment/web-service.png | Binary image file showing web service architecture |
| docs/4-advanced/05-integration-and-deployment/simple-forum-app-flow.drawio.svg | SVG diagram illustrating application data flow between user, servers, and database |
| docs/4-advanced/05-integration-and-deployment/origin-explanation.tsx | React component providing interactive explanation of URL origin components |
| docs/4-advanced/05-integration-and-deployment/_samples/fullstack-app/frontend/* | Frontend sample application configuration files including TypeScript configs, Vite setup, and package.json |
Files not reviewed (1)
- docs/4-advanced/05-integration-and-deployment/_samples/fullstack-app/backend/package-lock.json: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
nakaterm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
細かい部分のコラムでの説明も行き届いていてやっぱりさすがだなという感じです ✨
コメントしました!
| 1. バックエンドでTypeScriptを使用するためのセットアップを行います。 | ||
|
|
||
| ここに動画を埋め込む | ||
| 1. `cd backend`コマンドでカレントディレクトリを移動した後、`npm init`コマンドで`package.json`ファイルを作成し、`type`フィールドの値を`"module"`にします。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここはnpm initコマンドを実行する際にtypeフィールドの値を"module"にした方がいいかもです。
chelproc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
見ました!
ちなみに新しい技術の導入ですが、「XX は YYYY です」とすると「その定義あってる?」となりがちなので、「XX を用いると、YYYY できます」とか、「YYYY するには、XX を使用します」とかすると良かったりします。
|
|
||
| datasource db { | ||
| provider = "postgresql" | ||
| url = env("DATABASE_URL") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
↓のようなエラーが出るのですが大丈夫そうでしょうか??
The datasource property
urlis no longer supported in schema files. Move connection URLs for Migrate toprisma.config.tsand pass eitheradapterfor a direct database connection oraccelerateUrlfor Accelerate to thePrismaClientconstructor. See https://pris.ly/d/config-datasource and https://pris.ly/d/prisma7-client-configPrisma
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prisma 7.0.0以降だとそのようになるようです。Prisma 7.1.0に対応したことで、修正されました!
|
|
||
| app.post("/send", async (request, response) => { | ||
| await client.post.create({ data: { message: request.body.message } }); | ||
| response.send(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sendStatus(201); ?
| response.json(posts); | ||
| }); | ||
|
|
||
| app.post("/send", async (request, response) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RESTful なら POST /posts なのでそうすべき?
|
|
||
| ## フロントエンドとバックエンドを連携する | ||
|
|
||
| Reactによって作成されたブラウザ上で動くアプリケーションと、Node.jsによって作成されたサーバー上で動くアプリケーションを接続する方法について学びましょう。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
「フロントエンド」「バックエンド」という言葉は導入済みなので使ってしまっても良いのでは?
|
|
||
|  | ||
|
|
||
| 今回作成する掲示板サービスでは、フロントエンドとバックエンドでそれぞれ別のWebサーバーを起動します。バックエンドはNode.jsを用いてWebサーバーを起動し、フロントエンドはViteを用いて開発用サーバーを起動します。処理の流れは、次の図のようになります。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
「起動」は運用により過ぎた表現な気がします。また、フロントエンドは開発環境のみ触れられており本番環境がどう動作するのかが分かりづらい気がします。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここでは簡単のため開発環境のみの場合に絞って説明していたのですが、やはりわかりづらいため、単に「今回作成する掲示板サービスでは、フロントエンドとバックエンドでそれぞれ別のサーバーを構築します。処理の流れは、次の図のようになります。」のような説明にしてみます 👍
|
|
||
| Viteでは[`VITE_`で始まる<Term>環境変数</Term>を`import.meta.env`オブジェクトのプロパティとして利用](https://ja.vite.dev/guide/env-and-mode.html#env-variables)できます。例えば、`VITE_API_ENDPOINT`という<Term>環境変数</Term>がある場合、`import.meta.env.VITE_API_ENDPOINT`として参照できます。 | ||
|
|
||
| なお、Viteは起動時に`.env`ファイルの内容を自動的に読み込みます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 「Viteは起動時に
.envファイルの内容を環境変数として読み込みます。」が先の方が自然かなと。 - 「Viteでは - できます」の繋がりが若干不正確かなと。「Viteは、
VITE_で始まる環境変数を、アプリケーション内でimport.meta.envオブジェクトのプロパティとして利用できるようにします。」
|
|
||
| ## デプロイする | ||
|
|
||
| 今までは、バックエンドとフロントエンドのそれぞれで`npm run dev`コマンドを実行することで、バックエンドの場合にはTypeScriptファイルを直接実行してWebサーバーを起動し、フロントエンドの場合にはViteの開発用サーバーを起動していました。しかしながら、<Term>デプロイ</Term>する際には、これとは異なる方法を用いる必要があります。バックエンドの場合には、TypeScriptファイルをJavaScriptファイルに<Term>トランスパイル</Term>してから、そのJavaScriptファイルを実行します。フロントエンドの場合には、Viteにより出力されたファイルを配信します。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いずれも別に必須ではないです(バックエンドはtsxで問題なく動きますし、viteの開発用サーバーも本番用としても使えないこともないです)。「本番環境ではxxxすることが一般的です」という表現が妥当かなと!可能であれば、まず本番の説明があり、その後開発用サーバーがその代わりとしてどう動くのかを説明する順序の方が素直かなと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この節の構成が、開発環境を構築してから(開発環境の構成を理解してから)、本番環境を構築するという順番になっているため、先に本番環境の説明をすることは難しそうです。
|
|
||
| | 環境 | バックエンド | フロントエンド | | ||
| | -------- | --------------------------------------------------------------------------- | ---------------------------------- | | ||
| | 開発環境 | TypeScriptファイルを直接実行してWebサーバーを起動 | Viteの開発用サーバーを起動 | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 「TypeScriptファイルを直接実行」よりは「tsxパッケージを用いて実行」の方がよいかと思います。
- フロントエンドではRender自身がWebサーバーとして機能していることを明示した方が良いかもしれません。
|
|
||
| :::tip[`prisma generate`コマンド] | ||
|
|
||
| [`prisma generate`コマンド](https://www.prisma.io/docs/orm/reference/prisma-cli-reference#generate)は、`schema.prisma`ファイルの内容に基づいて`Prisma Client`クラスを生成します。通常、`schema.prisma`ファイルを編集するたびにこのコマンドを再度実行して`Prisma Client`クラスを再生成する必要があります。ただし、`prisma db push`コマンドを実行する際にも`Prisma Client`クラスは生成されるため、`prisma db push`コマンドを実行した場合には`prisma generate`コマンドを実行する必要はありません。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
「Prisma Clientクラス」は表現として適切でないかもです!
一応正確には「そのテーブル構造に基づいて専用のクライアントを生成します」とかになるのかなと。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
すみません...
PrismaClientクラスとPrisma Clientを完全に混同していました。
|
|
||
| ### ビルドの設定をする | ||
|
|
||
| フロントエンドの場合には、`npm run build`コマンドを実行することで、Viteにより<Term>トランスパイル</Term>とバンドルの結果が`dist`ディレクトリに格納されるのでした。<Term>デプロイ</Term>の際には、この`dist`ディレクトリを配信すればよいです。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- この文脈だとトランスパイルとバンドルを分けて語ることは少ない気がします。「ビルド結果」とかでいいんじゃないでしょうか。
- 「デプロイ」は本番環境を作る瞬間のことなので、「distディレクトリの配信」は「デプロイ後の状況」であって、「デプロイの際」ではないかなと思います。「本番環境では
distディレクトリの内容を配信」とかが良いかなと
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一応ビルドということばは初出だったためこのように書いたのですが、やはり、「ビルド結果」のように書きます。
本番環境ということばも初出ではありますが、表ですでに使ってしまっているため、ここでも使用しようかと思います。
|
今までのレビューの内容をすべて直しました。また、動画なども加え、完成しています。再度レビューをお願いします! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 25 out of 51 changed files in this pull request and generated 3 comments.
Files not reviewed (2)
- docs/4-advanced/04-react/_samples/todo-database/backend/package-lock.json: Language not supported
- docs/4-advanced/05-integration-and-deployment/_samples/fullstack-app/backend/package-lock.json: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1 @@ | |||
| VITE_API_ENDPOINT="http://localhost:3000" | |||
Copilot
AI
Dec 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
環境変数の値がダブルクォートで囲まれていますが、.envファイルでは通常クォートは不要です。VITE_API_ENDPOINT=http://localhost:3000のように記述すべきです。Viteはクォートを値の一部として解釈する可能性があります。
| VITE_API_ENDPOINT="http://localhost:3000" | |
| VITE_API_ENDPOINT=http://localhost:3000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これで正しいです
| onClick={async () => { | ||
| await fetch(`${import.meta.env.VITE_API_ENDPOINT}/posts`, { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ message: newPostContent }), | ||
| }); | ||
| }} |
Copilot
AI
Dec 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
送信ボタンをクリックした後、投稿一覧が自動的に更新されません。POSTリクエストが成功した後、投稿一覧を再取得するか、新しい投稿をローカルの状態に追加する処理が必要です。また、エラーハンドリングも実装されていません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
仕様です
|
|
||
| useEffect(() => { | ||
| async function fetchPosts() { | ||
| const response = await fetch( | ||
| `${import.meta.env.VITE_API_ENDPOINT}/posts`, | ||
| ); | ||
| setPosts(await response.json()); | ||
| } | ||
| fetchPosts(); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <> |
Copilot
AI
Dec 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fetchPosts関数でエラーハンドリングが実装されていません。ネットワークエラーやサーバーエラーが発生した場合、ユーザーにエラーメッセージを表示する必要があります。
| useEffect(() => { | |
| async function fetchPosts() { | |
| const response = await fetch( | |
| `${import.meta.env.VITE_API_ENDPOINT}/posts`, | |
| ); | |
| setPosts(await response.json()); | |
| } | |
| fetchPosts(); | |
| }, []); | |
| return ( | |
| <> | |
| const [error, setError] = useState<string | null>(null); | |
| useEffect(() => { | |
| async function fetchPosts() { | |
| try { | |
| const response = await fetch( | |
| `${import.meta.env.VITE_API_ENDPOINT}/posts`, | |
| ); | |
| if (!response.ok) { | |
| throw new Error(`Failed to fetch posts: ${response.status}`); | |
| } | |
| const data = await response.json(); | |
| setPosts(data); | |
| setError(null); | |
| } catch (err) { | |
| console.error("Error fetching posts:", err); | |
| setError( | |
| "投稿の取得中にエラーが発生しました。しばらくしてから再度お試しください。", | |
| ); | |
| } | |
| } | |
| fetchPosts(); | |
| }, []); | |
| return ( | |
| <> | |
| {error && ( | |
| <div role="alert"> | |
| {error} | |
| </div> | |
| )} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
仕様です
nakaterm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
動画もお疲れ様です!
細かいところばかりですがコメントしました!
| ```shell | ||
| $ npm install cors | ||
| $ npm install -D @types/cors | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
ここは
$が入っているが、「TypeScriptのセットアップをする」のところでは入っていなかったので統一すると良さそうです -
同様の内容をインラインコードで書いてある場所もあるのでどちらかに統一すると良さそうです (理由があって区別しているなら問題ないですが)
npm install expressコマンドとnpm install -D @types/expressコマンドを実行して、Expressとその型定義をインストールします。npm install @prisma/client @prisma/adapter-pg pg dotenvコマンドとnpm install -D prisma @types/pgコマンドを実行して、Prismaを利用するために必要なパッケージをインストールします。
|  | ||
|  | ||
|
|
||
| <ViewSource url={import.meta.url} path="_samples/fullstack-app" noCodeSandbox /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コードの編集は「ビルドの設定をする」までで完了するので、この ViewSource は「ビルドの設定をする」の末尾でもいいような気がしました。
|
|
||
| // Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? | ||
| // Try Prisma Accelerate: https://pris.ly/cli/accelerate-init | ||
| 1. カレントディレクトリを`backend`ディレクトリに移動し、`npm init`コマンドを実行して`package.json`ファイルを作成します。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
「カレントディレクトリを」って要りますかね?「〇〇ディレクトリに移動し」でいいような気がします。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
また、type を module にすることについて文面でも言及があったほうがいいような気がします。
| #### Reactのセットアップをする | ||
|
|
||
| ::: | ||
| カレントディレクトリをプロジェクトを格納するディレクトリに移動してから、`npm create vite@latest`コマンドを実行して`frontend`という名前でReactのプロジェクトを作成します。カレントディレクトリを`frontend`ディレクトリに移動してから、`npm install`コマンドを実行して必要なパッケージをインストールします。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上 (「カレントディレクトリを」)
| provider = "postgresql" | ||
| url = env("DATABASE_URL") | ||
| } | ||
| #### データベースと開発用サーバーを作成する |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Express で動かすバックエンドのサーバーは「Web サーバー」でいいような?
過去のコメント ( #890 (comment) )見ましたが、これ実はコメントつける場所が間違っているんじゃないですかね? 文面見る限り、フロントエンドの Vite のことだけを指しているような気がします (cc: @chelproc )
この節で書く内容の概略と節構成は #886 を参照してください。
「ここに動画を埋め込む」と記載されているところに、今後動画を掲載します。動画の掲載以外については現在すべて完了しています。
Prismaの使用法については、最新のPrisma 7.1.0の使用法に合わせて記述しています。現在のut.code(); LearnでのPrismaの使用法はPrisma 6.18以前の使用法であることに注意してください。こちらについての情報は #892 にまとめているので、参照してください。