欢迎阅读有关使用 GitHub Actions、Node.js、自动化博客文章应用程序部署的指南
CouchDB 和 Aptible。
这个综合教程将指导您使用上述工具和技术构建、部署和管理博客文章应用程序。
但首先,让我简要概述一下Blogpost应用程序、它的功能及其主要组件。 Blogpost应用程序是一个 Web 应用程序,允许用户创建和共享博客文章。
用户可以撰写、编辑、删除和查看其他用户的帖子。该应用程序使用 Node.js 作为后端,CouchDB 作为数据库,GitHub Actions 作为持续集成和部署工具。
我为什么选择这些?原因有很多,但以下是一些主要的原因:
在开始开发之旅之前,设置必要的工具和技术至关重要。
npm init -y
npm install express nano
http://127.0.0.1:5984/_utils/
/ 。
blog.js
文件。
const express = require("express"); const nano = require("nano")("http://admin:[email protected]:5984"); const app = express(); const port = 3000; // Middleware to parse JSON data app.use(express.json()); // async function asyncCall() { // // await nano.db.destroy("alice"); // await nano.db.create("blogposts"); // const db = nano.use("blogposts"); // return response; // } // asyncCall(); const db = nano.use("blogposts");
此代码定义了用于使用 CouchDB 数据库创建、检索、更新和删除博客文章的 API 端点。
// Create a new blog post app.post("/posts", async (req, res) => { const { title, description, author } = req.body; try { const doc = await db.insert({ title, description, author, createdAt: new Date(), updatedAt: new Date(), }); res.json({ id: doc.id, rev: doc.rev }); } catch (err) { console.error(err); res.status(500).send("Error creating post"); } }); // Get all blog posts app.get("/posts", async (req, res) => { try { const docs = await db.list({ include_docs: true }); res.json(docs.rows); } catch (err) { console.error(err); res.status(500).send("Error retrieving posts"); } }); // Get a specific blog post app.get("/posts/:id", async (req, res) => { const { id } = req.params; try { const doc = await db.get(id); res.json(doc); } catch (err) { console.error(err); res.status(404).send("Post not found"); } }); // Update a blog post app.put("/posts/:id", async (req, res) => { const { id } = req.params; const { title, description } = req.body; try { await db.insert({ _id: id, title, description, updatedAt: new Date(), }); res.json({ message: "Post updated successfully" }); } catch (err) { console.error(err); res.status(500).send("Error updating post"); } }); // Delete a blog post app.delete("/posts/:id", async (req, res) => { const { id } = req.params; try { await db.destroy(id); res.json({ message: "Post deleted successfully" }); } catch (err) { console.error(err); res.status(500).send("Error deleting post"); } }); app.listen(port, () => { console.log(`Blogpost app listening on port ${port}`); });
彻底的测试对于确保项目的功能和稳健性至关重要。以下是指导您的测试过程的示例 API 文档:
基本网址:
假设您的服务器在端口 3000 本地运行,您的 API 的基本 URL 将为:
http://localhost:3000
端点: POST /posts
描述:创建新博客文章。
请求正文:
{ "title": "String", "description": "String", "author": "String" }
例子:
POST http://localhost:3000/posts { "title": "Sample Title", "description": "Sample Description", "author": "John Doe" }
端点: GET /posts
描述:检索所有博客文章。
回复:
[ { "id": "String", "key": "String", "value": { "rev": "String" }, "doc": { "_id": "String", "_rev": "String", "title": "String", "description": "String", "author": "String", "createdAt": "Date", "updatedAt": "Date" } } ]
例子:
GET http://localhost:3000/posts
端点: GET /posts/:id
描述:通过 ID 检索特定博客文章。
参数:
id
:博客文章的 ID。回复:
{ "_id": "String", "_rev": "String", "title": "String", "description": "String", "author": "String", "createdAt": "Date", "updatedAt": "Date" }
例子:
GET http://localhost:3000/posts/your-post-id
端点: PUT /posts/:id
描述:通过 ID 更新特定博客文章。
参数:
id
:博客文章的 ID。请求正文:
{ "title": "String", "description": "String" }
例子:
PUT http://localhost:3000/posts/your-post-id { "title": "Updated Title", "description": "Updated Description" }
端点: DELETE /posts/:id
描述:按 ID 删除特定博客文章。
参数:
id
:博客文章的 ID。例子:
DELETE http://localhost:3000/posts/your-post-id
测试GET
、 PUT
和DELETE
请求时,请将your-post-id
替换为博客文章的实际 ID。
您需要有一个 Docker Hub 帐户。如果您尚未创建,请在 Docker Hub 注册。
确保您已在本地计算机上安装并运行 Docker。
将 Docker 化应用程序推送到 Docker Hub 的步骤:
Dockerfile
。 # Use an official Node.js runtime as the base image FROM node:16 # Set the working directory in the container WORKDIR /usr/src/app # Copy package.json and package-lock.json to the working directory COPY package*.json ./ # Install app dependencies RUN npm install # Copy the rest of the application files to the working directory COPY . . # Expose the port the app runs on EXPOSE 3000 # Define the command to run the app CMD ["node", "blog.js"]
标记您的 Docker 映像:打开终端/命令提示符并导航到 Dockerfile 所在的 Node.js 应用程序的根目录。
运行以下命令来构建 Docker 映像,并使用您的 Docker Hub 用户名和所需的存储库名称对其进行标记:
docker build -t your-docker-username/blogpost-app:latest .
将your-docker-username
替换为您的 Docker Hub 用户名,将blogpost-app
替换为您所需的存储库名称。
您将得到类似的响应,如下所示:
[+] Building 1.1s (10/10) FINISHED docker:desktop-linux => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 489B 0.0s => [internal] load metadata for docker.io/library/node:16 1.0s => [1/5] FROM docker.io/library/node:16@sha256:f77a1aef2da8d83e45ec990f45df50f1a286c5fe8bbfb8c6e4246c6389705c0b 0.0s => [internal] load build context 0.0s => => transferring context: 45.31kB 0.0s => CACHED [2/5] WORKDIR /usr/src/app 0.0s => CACHED [3/5] COPY package*.json ./ 0.0s => CACHED [4/5] RUN npm install 0.0s => CACHED [5/5] COPY . . 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:c5f046a9b99389aea6bf3f503e9b05cce953daf1b3f77ee5fb3f7469dc36c709 0.0s => => naming to docker.io/wise4rmgod/blogpost-app:latest
docker login
出现提示时输入您的 Docker Hub 用户名和密码。
Authenticating with existing credentials... Login Succeeded
docker push your-docker-username/blogpost-app:latest
此命令将本地镜像上传到指定存储库下的 Docker Hub。
验证推送:转到您在网络上的 Docker Hub 帐户,然后导航到您的存储库以确认您的 Docker 映像已成功推送。
aptible deploy --app reactgame --docker-image wise4rmgod/blogpost-app --private-registry-username wise4rmgod --private-registry-password H$(_tS+W~ZBST63
本教程假设您对在 Aptible 平台上设置环境、应用程序、端点和数据库有基本的了解。本教程使用 CouchDB 作为数据库,并使用 Direct Docker 进行部署。
aptible login
系统将提示您输入电子邮件和密码。如果成功,您将收到类似于以下内容的响应:
Token written to /Users/wisdomnwokocha/.aptible/tokens.json This token will expire after 6 days, 23 hrs (use --lifetime to customize)
句法:
aptible deploy --app <app name> --docker-image <docker image in cloud>
这是一个示例命令:
aptible deploy --app reactgame --docker-image wise4rmgod/blogpost-app
您将收到类似于以下内容的回复:
INFO -- : Starting App deploy operation with ID: 61135861 INFO -- : Deploying without git repository INFO -- : Writing .aptible.env file... INFO -- : Fetching app image: wise4rmgod/blogpost-app... INFO -- : Pulling from wise4rmgod/blogpost-app INFO -- : 26ee4ff96582: Pulling fs layer INFO -- : 446eab4103f4: Pulling fs layer INFO -- : 2e3c22a0f840: Pulling fs layer INFO -- : a7ab8ad9b408: Pulling fs layer INFO -- : 3808fdf0c601: Pulling fs layer INFO -- : ab9e4075c671: Pulling fs layer INFO -- : 362360c8cef6: Pulling fs layer INFO -- : 928b5d11ac66: Pulling fs layer INFO -- : dc87e077ac61: Pulling fs layer INFO -- : f108e80f4efc: Pulling fs layer INFO -- : 84ac53840ac8: Pulling fs layer INFO -- : e81f21b79a1f: Pulling fs layer INFO -- : 2e3c22a0f840: Downloading: 523 KB / 49.8 MB INFO -- : 446eab4103f4: Downloading: 173 KB / 16.6 MB INFO -- : 26ee4ff96582: Downloading: 483 KB / 47 MB INFO -- : 2e3c22a0f840: Downloading: 25.7 MB / 49.8 MB INFO -- : a7ab8ad9b408: Downloading: 528 KB / 175 MB INFO -- : ab9e4075c671: Downloading: 355 KB / 33.4 MB INFO -- : a7ab8ad9b408: Downloading: 35.3 MB / 175 MB INFO -- : 26ee4ff96582: Pull complete INFO -- : 446eab4103f4: Pull complete INFO -- : 2e3c22a0f840: Pull complete INFO -- : a7ab8ad9b408: Downloading: 71.2 MB / 175 MB INFO -- : a7ab8ad9b408: Downloading: 106 MB / 175 MB INFO -- : a7ab8ad9b408: Downloading: 142 MB / 175 MB INFO -- : a7ab8ad9b408: Pull complete INFO -- : 3808fdf0c601: Pull complete INFO -- : ab9e4075c671: Pull complete INFO -- : 362360c8cef6: Pull complete INFO -- : 928b5d11ac66: Pull complete INFO -- : dc87e077ac61: Pull complete INFO -- : f108e80f4efc: Pull complete INFO -- : 84ac53840ac8: Pull complete INFO -- : e81f21b79a1f: Pull complete INFO -- : Digest: sha256:de9d04d069ca89ebdb37327365a815c88cd40d90cbc5395cc31c351fff1206dd INFO -- : Status: Downloaded newer image for wise4rmgod/blogpost-app:latest INFO -- : No Procfile found in git directory or /.aptible/Procfile found in Docker image: using Docker image CMD INFO -- : No .aptible.yml found in git directory or /.aptible/.aptible.yml found in Docker image: no before_release commands will run INFO -- : Pushing image dualstack-v2-registry-i-0a5ec8cff8e775b34.aptible.in:46022/app-63213/72184c41-7dc6-4313-b10e-749125f72577:latest to private Docker registry... INFO -- : The push refers to repository [dualstack-v2-registry-i-0a5ec8cff8e775b34.aptible.in:46022/app-63213/72184c41-7dc6-4313-b10e-749125f72577] INFO -- : dd387bc6b362: Pushed INFO -- : 586bd9d5efcf: Pushed INFO -- : 8ae0c889ca84: Pushed INFO -- : c91ec53bcc27: Pushing: 522 KB / 93.6 MB INFO -- : aec897bac4f0: Pushed INFO -- : 0ead224631d3: Pushed INFO -- : ad3b30eb29d3: Pushing: 542 KB / 444 MB INFO -- : 2a7587eb01b6: Pushing: 544 KB / 137 MB INFO -- : be36d2a441aa: Pushed INFO -- : 03f6e3800bbe: Pushed INFO -- : a10e482288d1: Pushing: 338 KB / 30.7 MB INFO -- : f9cfc9f6b603: Pushing: 513 KB / 103 MB INFO -- : c91ec53bcc27: Pushing: 31.3 MB / 93.6 MB INFO -- : c91ec53bcc27: Pushing: 62.7 MB / 93.6 MB INFO -- : ad3b30eb29d3: Pushing: 44.5 MB / 444 MB INFO -- : 2a7587eb01b6: Pushing: 34.4 MB / 137 MB INFO -- : a10e482288d1: Pushed INFO -- : ad3b30eb29d3: Pushing: 88.9 MB / 444 MB INFO -- : f9cfc9f6b603: Pushing: 34.6 MB / 103 MB INFO -- : 2a7587eb01b6: Pushing: 68.9 MB / 137 MB INFO -- : ad3b30eb29d3: Pushing: 133 MB / 444 MB INFO -- : f9cfc9f6b603: Pushing: 70.2 MB / 103 MB INFO -- : c91ec53bcc27: Pushed INFO -- : 2a7587eb01b6: Pushing: 103 MB / 137 MB INFO -- : ad3b30eb29d3: Pushing: 178 MB / 444 MB INFO -- : ad3b30eb29d3: Pushing: 224 MB / 444 MB INFO -- : 2a7587eb01b6: Pushed INFO -- : f9cfc9f6b603: Pushed INFO -- : ad3b30eb29d3: Pushing: 270 MB / 444 MB INFO -- : ad3b30eb29d3: Pushing: 312 MB / 444 MB INFO -- : ad3b30eb29d3: Pushing: 355 MB / 444 MB INFO -- : ad3b30eb29d3: Pushing: 401 MB / 444 MB INFO -- : ad3b30eb29d3: Pushed INFO -- : latest: digest: sha256:de9d04d069ca89ebdb37327365a815c88cd40d90cbc5395cc31c351fff1206dd size: 2841 INFO -- : Pulling from app-63213/72184c41-7dc6-4313-b10e-749125f72577 INFO -- : Digest: sha256:de9d04d069ca89ebdb37327365a815c88cd40d90cbc5395cc31c351fff1206dd INFO -- : Status: Image is up to date for dualstack-v2-registry-i-0a5ec8cff8e775b34.aptible.in:46022/app-63213/72184c41-7dc6-4313-b10e-749125f72577:latest INFO -- : Image app-63213/72184c41-7dc6-4313-b10e-749125f72577 successfully pushed to registry. INFO -- : STARTING: Register service cmd in API INFO -- : COMPLETED (after 0.28s): Register service cmd in API INFO -- : STARTING: Derive placement policy for service cmd INFO -- : COMPLETED (after 0.15s): Derive placement policy for service cmd INFO -- : STARTING: Create new release for service cmd INFO -- : COMPLETED (after 0.24s): Create new release for service cmd INFO -- : STARTING: Schedule service cmd .. INFO -- : COMPLETED (after 13.49s): Schedule service cmd INFO -- : STARTING: Stop old app containers for service cmd INFO -- : COMPLETED (after 0.0s): Stop old app containers for service cmd INFO -- : STARTING: Start app containers for service cmd INFO -- : WAITING FOR: Start app containers for service cmd INFO -- : WAITING FOR: Start app containers for service cmd INFO -- : COMPLETED (after 18.4s): Start app containers for service cmd INFO -- : STARTING: Delete old containers for service cmd in API INFO -- : COMPLETED (after 0.0s): Delete old containers for service cmd in API INFO -- : STARTING: Commit app containers in API for service cmd INFO -- : COMPLETED (after 0.26s): Commit app containers in API for service cmd INFO -- : STARTING: Commit service cmd in API INFO -- : COMPLETED (after 0.13s): Commit service cmd in API INFO -- : STARTING: Cache maintenance page INFO -- : COMPLETED (after 0.28s): Cache maintenance page INFO -- : STARTING: Commit app in API INFO -- : COMPLETED (after 0.19s): Commit app in API INFO -- : App deploy successful.
这个综合教程将帮助您使用 Node.js、CouchDB 和 Aptible 构建、部署和管理博客文章应用程序。
您已经掌握了设置基本工具和技术、构建博客文章应用程序的后端、对应用程序进行 docker 化、将 Docker 映像推送到 Docker Hub 以及将应用程序部署到 Aptible 的基础知识。
祝贺您完成本综合教程并开启使用 Aptible 进行云原生应用程序开发的旅程!