feat: 新增 node-frontend-vue3 示例(Vue 3 + Element Plus,Tengine 托管)

This commit is contained in:
2026-11-15 11:50:49 +08:00
parent 793bcf10a1
commit 92a57d99a0
19 changed files with 475 additions and 0 deletions
@@ -0,0 +1,4 @@
dist/
node_modules/
.git/
.DS_Store
+3
View File
@@ -0,0 +1,3 @@
dist/
node_modules/
npm-debug.log*
+23
View File
@@ -0,0 +1,23 @@
# 编译阶段:compile-node22(Vite 7 需要 Node >= 20.19)
FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-node22 AS build
WORKDIR /src
COPY package.json package-lock.json* ./
# npm 源由镜像的 /usr/local/node/etc/npmrc 指向 npmmirror,无需额外换源
RUN npm install --no-audit --no-fund
COPY . ./
RUN npm run build && ls -lh dist/ dist/assets/
# 运行阶段:Tengine 3.1.0(自建 proxy-tengine3 镜像)托管静态文件
FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:proxy-tengine3 AS runtime
LABEL MAINTAINER="tianjun@odboy.cn"
ENV TimeZone=Asia/Shanghai
# 站点配置与构建产物都放进镜像:conf.d/*.conf 由主配置 include,html/ 为站点根目录
COPY nginx/node-frontend-vue3.conf /usr/local/nginx/conf/conf.d/node-frontend-vue3.conf
COPY --from=build /src/dist/ /usr/local/nginx/html/
# 构建期自检:配置语法错误直接让镜像构建失败;纯静态站点没有上游主机名,nginx -t 可直接通过
RUN /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
EXPOSE 80
STOPSIGNAL SIGQUIT
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
+74
View File
@@ -0,0 +1,74 @@
# node-frontend-vue3 示例
Vue 3 + Element Plus 的前端项目,**构建阶段**用 `compile-node22` 跑 Vite,**运行阶段**以自建
`proxy-tengine3` 镜像为基座托管静态文件。与 [`node-frontend-vue2`](../node-frontend-vue2/README.md) 对照,
差别只在框架版本与 UI 库。
| 阶段 | 镜像 | 说明 |
| --- | --- | --- |
| 编译 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-node22` | Node 22 + npm(源为 npmmirror),Vite 7 要求 Node ≥ 20.19 |
| 运行 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:proxy-tengine3` | Tengine 3.1.0,托管 `dist/` |
纯前端项目:页面数据全部在浏览器内产生,不调用任何后端接口,部署只需要一个 Tengine 容器。
### 示例内容
```
node-frontend-vue3/
├── package.json # vue 3.5.45 / element-plus 2.14.8 / vue-router 4.6.5;vite 7 + plugin-vue 6
├── vite.config.js # base: './';element-plus 单独拆包
├── index.html
├── src/
│ ├── main.js # createApp().use(router).use(ElementPlus)
│ ├── router.js # createWebHashHistory,/ /form /about
│ ├── App.vue # <script setup>,顶部导航 + router-view
│ └── views/
│ ├── HomeView.vue # ref/computed + onMounted 时钟,el-statistic / el-rate / el-progress
│ ├── FormView.vue # el-form 校验、el-select、el-date-picker,value-format="YYYY-MM-DD"
│ └── AboutView.vue # 版本与部署说明
├── nginx/
│ └── node-frontend-vue3.conf # Tengine 站点配置:静态托管 + /healthz
├── Dockerfile # 两阶段:compile-node22 -> proxy-tengine3
├── build.sh # 构建镜像(PUSH=1 时同时推送)
├── docker-compose.yml # 只部署前端(端口 8090)
├── startup.sh / shutdown.sh
└── .dockerignore / .gitignore
```
### 构建与部署
```shell
sh build.sh # docker build -t registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vue3 .
PUSH=1 sh build.sh # 构建并推送
sh startup.sh # docker-compose -f docker-compose.yml -p node-frontend-vue3-demo up -d
```
访问 http://127.0.0.1:8090/ ;`curl http://127.0.0.1:8090/healthz` 返回 `ok`。
### 本地开发(不用 docker)
```shell
npm install
npm run dev # http://127.0.0.1:5273
```
### 关键点
**为什么用 Tengine 托管而不是 Node 托管**:Vue 打包后是纯静态文件,用 `proxy-tengine3` 直接托管,
运行阶段不保留 Node/npm,镜像更小,也能顺手用 Tengine 做 gzip 与缓存。
**站点配置**:配置放在 `nginx/node-frontend-vue3.conf`,构建时 `COPY` 到 `/usr/local/nginx/conf/conf.d/`
(镜像自带 nginx.conf 已经 `include conf.d/*.conf`);compose 又把宿主机 `./nginx` 挂到同一个 `conf.d`,
改完执行 `nginx -t && nginx -s reload` 即生效。挂目录而不是单文件:单文件挂载编辑会换 inode,容器内看不到改动。
**路由模式**:`createWebHashHistory`,部署到任何路径都不用 rewrite;要换 history 模式,
`try_files` 已经写好,改 `router.js` 即可。
**与 Vue 2 版差异**:`<script setup>`、`ref/computed`、`createApp`、Element Plus 的
`el-date-picker` 用 `value-format="YYYY-MM-DD"`(Vue 2 + Element UI 是 `yyyy-MM-dd`)。
### 停止
```shell
sh shutdown.sh
```
+11
View File
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# 在示例目录下构建示例镜像(编译 + 运行两阶段均在本机 docker 内完成)
set -euo pipefail
cd "$(dirname "$0")"
IMAGE=${IMAGE:-registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vue3}
docker build -t "${IMAGE}" .
if [ "${PUSH:-0}" = "1" ]; then
docker push "${IMAGE}"
fi
@@ -0,0 +1,32 @@
version: '3.8'
# 网段避开其它示例(172.31.0/24 ~ 172.31.3/24 已被 go / rust / node-backend / node-frontend-vue2 占用)
networks:
node-frontend-vue3-demo:
ipam:
driver: default
config:
- subnet: "172.31.4.0/24"
services:
frontend:
image: registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vue3 # 镜像由 build.sh 构建
container_name: node-frontend-vue3-demo
restart: unless-stopped
ports: # 映射端口:前端页面
- "8090:80"
volumes: # 只挂载站点配置目录,改完 reload 即可,不必重建镜像
# 挂目录而不是单文件:单文件挂载在宿主机上编辑会换 inode,容器内看不到改动
- "./nginx:/usr/local/nginx/conf/conf.d"
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
healthcheck:
# 运行镜像自带 net-tools,直接用端口探测
test: ["CMD-SHELL", "netstat -tln | grep -q ':80 '"]
interval: 30s
timeout: 5s
retries: 3
start_period: 5s
networks:
node-frontend-vue3-demo:
ipv4_address: 172.31.4.20
+12
View File
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>node-frontend-vue3 | Vue 3 + Element Plus</title>
</head>
<body>
<div id="app"></div>
<script src="/src/main.js" type="module"></script>
</body>
</html>
@@ -0,0 +1,29 @@
# node-frontend-vue3 站点:Tengine 3.1.0 托管 Vue 3 构建产物(纯静态,无后端依赖)
server {
listen 80;
server_name _;
access_log /dev/stdout main;
root /usr/local/nginx/html;
index index.html;
# 静态资源带 hash,长缓存;index.html 每次重新验证,保证发版后立即生效
location /assets/ {
expires 30d;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
# 前端路由兜底:hash 模式下用不到,保留是为了以后切 history 模式时不用改配置
location / {
try_files $uri $uri/ /index.html;
}
# 健康检查:直接读 index.html
location = /healthz {
access_log off;
default_type text/plain;
return 200 "ok\n";
}
}
+24
View File
@@ -0,0 +1,24 @@
{
"name": "node-frontend-vue3",
"version": "1.0.0",
"private": true,
"description": "Vue 3 + Element Plus 前端示例,构建产物由 Tengine 托管",
"type": "module",
"engines": {
"node": ">=20.19"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview --port 4173"
},
"dependencies": {
"element-plus": "2.14.8",
"vue": "3.5.45",
"vue-router": "4.6.5"
},
"devDependencies": {
"@vitejs/plugin-vue": "6.0.11",
"vite": "7.3.8"
}
}
+4
View File
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
docker-compose -f docker-compose.yml -p node-frontend-vue3-demo down
+19
View File
@@ -0,0 +1,19 @@
<template>
<el-container class="app">
<el-header height="56px" class="header">
<span class="logo">node-frontend-vue3</span>
<el-menu mode="horizontal" :default-active="$route.path" router :ellipsis="false">
<el-menu-item index="/">首页</el-menu-item>
<el-menu-item index="/form">表单</el-menu-item>
<el-menu-item index="/about">关于</el-menu-item>
</el-menu>
</el-header>
<el-main>
<router-view />
</el-main>
</el-container>
</template>
<script setup>
// Vue 3 单文件组件 + 组合式 API,脚本部分刻意保持最小
</script>
+10
View File
@@ -0,0 +1,10 @@
import { createApp } from 'vue';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import './styles.css';
import App from './App.vue';
import router from './router';
// Vue 3 组合式写法 + Element Plus
createApp(App).use(router).use(ElementPlus).mount('#app');
+15
View File
@@ -0,0 +1,15 @@
import { createRouter, createWebHashHistory } from 'vue-router';
import Home from './views/HomeView.vue';
const routes = [
{ path: '/', name: 'home', component: Home },
{ path: '/form', name: 'form', component: () => import('./views/FormView.vue') },
{ path: '/about', name: 'about', component: () => import('./views/AboutView.vue') },
// 前端路由兜底:hash 模式下用不到,Tengine 的 location / 里已经配了 try_files
{ path: '/:pathMatch(.*)*', redirect: '/' },
];
export default createRouter({
history: createWebHashHistory(),
routes,
});
@@ -0,0 +1,20 @@
html,
body,
#app {
height: 100%;
margin: 0;
}
.header {
display: flex;
align-items: center;
gap: 24px;
background: #fff;
border-bottom: 1px solid var(--el-border-color-light);
}
.logo {
font-size: 18px;
font-weight: 600;
color: var(--el-color-primary);
}
@@ -0,0 +1,16 @@
<template>
<el-card shadow="never">
<template #header><span>关于</span></template>
<el-descriptions :column="1" border>
<el-descriptions-item label="框架">Vue 3.5.45(组合式 API,&lt;script setup&gt;)</el-descriptions-item>
<el-descriptions-item label="组件库">Element Plus 2.14.8</el-descriptions-item>
<el-descriptions-item label="构建">Vite 7 + @vitejs/plugin-vue 6</el-descriptions-item>
<el-descriptions-item label="部署">Tengine 3.1.0(proxy-tengine3 镜像)托管 dist/</el-descriptions-item>
<el-descriptions-item label="路由模式">hash(无需服务端 rewrite)</el-descriptions-item>
</el-descriptions>
</el-card>
</template>
<script setup>
// 静态说明页,无逻辑
</script>
@@ -0,0 +1,55 @@
<template>
<el-card shadow="never">
<template #header><span>Element Plus 表单</span></template>
<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
<el-form-item label="名称" prop="name">
<el-input v-model="form.name" placeholder="请输入名称" />
</el-form-item>
<el-form-item label="类型" prop="type">
<el-select v-model="form.type" placeholder="请选择">
<el-option label="前端" value="frontend" />
<el-option label="后端" value="backend" />
</el-select>
</el-form-item>
<el-form-item label="启用">
<el-switch v-model="form.enabled" />
</el-form-item>
<el-form-item label="日期">
<el-date-picker v-model="form.date" type="date" value-format="YYYY-MM-DD" placeholder="选择日期" />
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submit">提交</el-button>
<el-button @click="reset">重置</el-button>
</el-form-item>
</el-form>
<el-alert v-if="result" :title="result" type="success" :closable="false" show-icon />
</el-card>
</template>
<script setup>
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
const emptyForm = () => ({ name: '', type: '', enabled: true, date: '' });
const formRef = ref(null);
const form = reactive(emptyForm());
const result = ref('');
const rules = {
name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
type: [{ required: true, message: '请选择类型', trigger: 'change' }],
};
function submit() {
formRef.value.validate((ok) => {
if (!ok) return;
result.value = `已提交:${JSON.stringify(form)}`;
ElMessage.success('提交成功');
});
}
function reset() {
formRef.value.resetFields();
result.value = '';
}
</script>
@@ -0,0 +1,94 @@
<template>
<div>
<el-row :gutter="16">
<el-col :span="8">
<el-card shadow="never">
<template #header><span>运行时</span></template>
<el-descriptions :column="1" border>
<el-descriptions-item label="Vue">{{ vueVersion }}</el-descriptions-item>
<el-descriptions-item label="Element Plus">2.14.8</el-descriptions-item>
<el-descriptions-item label="路由">createWebHashHistory</el-descriptions-item>
</el-descriptions>
</el-card>
</el-col>
<el-col :span="8">
<el-card shadow="never">
<template #header><span>计数器(ref)</span></template>
<el-statistic :value="count" title="当前值" />
<div style="margin-top: 16px">
<el-button size="small" @click="count -= 1">-1</el-button>
<el-button size="small" type="primary" @click="count += 1">+1</el-button>
<el-button size="small" @click="count = 0">重置</el-button>
</div>
</el-card>
</el-col>
<el-col :span="8">
<el-card shadow="never">
<template #header><span>当前时间</span></template>
<p class="clock">{{ clock }}</p>
<el-tag type="success" size="small">前端本地时间,不请求后端</el-tag>
</el-card>
</el-col>
</el-row>
<el-card shadow="never" style="margin-top: 16px">
<template #header><span>Element Plus 组件一览</span></template>
<el-space wrap>
<el-button>默认</el-button>
<el-button type="primary">主要</el-button>
<el-button type="success">成功</el-button>
<el-tag>标签</el-tag>
<el-tag type="info" effect="plain">描边</el-tag>
<el-switch v-model="enabled" />
<el-rate v-model="rate" />
<el-progress :percentage="percent" style="width: 200px" />
</el-space>
<el-divider />
<el-input v-model="keyword" placeholder="输入关键字过滤,纯前端" clearable style="max-width: 320px" />
<el-alert
v-if="keyword"
style="margin-top: 12px"
:title="`已输入:${keyword}`"
type="info"
:closable="false"
show-icon
/>
</el-card>
</div>
</template>
<script setup>
import { onBeforeUnmount, onMounted, ref, version as vueVersionValue } from 'vue';
// 纯前端展示:所有数据都在浏览器里产生,不调用任何后端接口
const vueVersion = vueVersionValue;
const count = ref(0);
const clock = ref(format(new Date()));
const enabled = ref(true);
const rate = ref(4);
const percent = ref(60);
const keyword = ref('');
let timer = null;
function format(d) {
const p = (n) => String(n).padStart(2, '0');
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
}
onMounted(() => {
timer = setInterval(() => {
clock.value = format(new Date());
}, 1000);
});
onBeforeUnmount(() => clearInterval(timer));
</script>
<style scoped>
.clock {
margin: 0 0 12px;
font-size: 24px;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
</style>
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
docker-compose -f docker-compose.yml -p node-frontend-vue3-demo up -d
docker-compose -f docker-compose.yml -p node-frontend-vue3-demo ps
@@ -0,0 +1,25 @@
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
// 纯静态前端:产物由 Tengine 托管,base 用相对路径,部署到任意子路径都能跑
export default defineConfig({
plugins: [vue()],
base: './',
server: {
host: '0.0.0.0',
port: 5273,
},
build: {
outDir: 'dist',
sourcemap: false,
// element-plus 体积较大,单独拆包,利用浏览器缓存
rollupOptions: {
output: {
manualChunks: {
'element-plus': ['element-plus'],
vue: ['vue', 'vue-router'],
},
},
},
},
});