feat: 新增 node-frontend-vue2 示例(Vue 2.7 + Element UI 2,Tengine 托管)

This commit is contained in:
2026-11-15 11:49:00 +08:00
parent a11fb1bb74
commit 793bcf10a1
19 changed files with 521 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*
+24
View File
@@ -0,0 +1,24 @@
# 编译阶段: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-vue2.conf /usr/local/nginx/conf/conf.d/node-frontend-vue2.conf
COPY --from=build /src/dist/ /usr/local/nginx/html/
# 构建期自检:配置语法错误直接让镜像构建失败
# 纯静态站点,配置里没有 proxy_pass 上游主机名,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;"]
+85
View File
@@ -0,0 +1,85 @@
# node-frontend-vue2 示例
Vue 2.7 + Element UI 2 的前端项目,**构建阶段**用 `compile-node22` 跑 Vite,**运行阶段**直接以自建
`proxy-tengine3` 镜像为基座托管静态文件,不再额外起 Node 容器。
| 阶段 | 镜像 | 说明 |
| --- | --- | --- |
| 编译 | `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-vue2/
├── package.json # vue 2.7.16 / element-ui 2.15.14 / vue-router 3.6.5;vite 7 + plugin-vue2
├── vite.config.js # base: './';element-ui 单独拆包
├── index.html
├── src/
│ ├── main.js # Vue.use(ElementUI)
│ ├── router.js # hash 模式,/ /form /about
│ ├── App.vue # 顶部导航 + router-view
│ └── views/
│ ├── Home.vue # 计数器、时钟、el-statistic / el-rate / el-progress 等组件一览
│ ├── Form.vue # el-form 校验、el-select、el-date-picker 等常用组件
│ └── About.vue # 版本与部署说明
├── nginx/
│ └── node-frontend-vue2.conf # Tengine 站点配置:静态托管 + /healthz
├── Dockerfile # 两阶段:compile-node22 -> proxy-tengine3
├── build.sh # 构建镜像(PUSH=1 时同时推送)
├── docker-compose.yml # 只部署前端(端口 8089)
├── startup.sh / shutdown.sh
└── .dockerignore / .gitignore
```
### 构建与部署
```shell
sh build.sh # docker build -t registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vue2 .
PUSH=1 sh build.sh # 构建并推送
sh startup.sh # docker-compose -f docker-compose.yml -p node-frontend-vue2-demo up -d
```
访问 http://127.0.0.1:8089/ :首页、表单页、关于页都是纯前端页面,不依赖任何后端容器。
```shell
curl http://127.0.0.1:8089/healthz # ok
```
### 本地开发(不用 docker)
```shell
npm install
npm run dev # http://127.0.0.1:5173
```
### 关键点
**为什么用 Tengine 托管而不是 Node 托管**:Vue 打包后是纯静态文件,用 `proxy-tengine3` 直接托管,
运行阶段不保留 Node/npm,镜像更小,也能顺手用 Tengine 做 gzip、缓存和反向代理。
**站点配置**:配置放在 `nginx/node-frontend-vue2.conf`。构建时文件被 `COPY` 到 `/usr/local/nginx/conf/conf.d/`
(镜像自带的 nginx.conf 已经 `include conf.d/*.conf`);compose 又把宿主机的 `./nginx` 目录挂载到同一个
`conf.d`,所以改完配置执行
`docker exec node-frontend-vue2-demo nginx -t && docker exec node-frontend-vue2-demo nginx -s reload` 即生效,不必重建镜像。
挂目录而不是挂单文件:单文件挂载在宿主机上编辑会换 inode,容器内看不到改动。
`/assets/` 长缓存(文件带 hash),`/` 用 `try_files $uri $uri/ /index.html` 兜底。
**路由模式**:示例用 hash 模式,部署到任何路径都不用额外 rewrite;要换 history 模式,`try_files` 已经写好,
改 `router.js` 的 `mode` 即可。
**前后端分离部署**:本示例是纯静态前端,只负责页面渲染,不代理任何接口。实际项目里后端是独立服务、
配有自己的域名(如 `api.example.com`),由前端直接跨域调用或由统一的网关处理;本仓库不预置任何后端容器,
也不在站点配置里写 `proxy_pass` 上游——这样构建阶段不在业务网络里也能直接 `nginx -t` 通过。
如果某个部署环境确实需要同域转发,再在那个环境的配置里单独加 `location` 即可,不影响本示例。
**依赖版本**:Vue 2.7.16 是 Vue 2 的最后一个版本(内置组合式 API,但示例仍用选项式写法),
`@vitejs/plugin-vue2` 负责让 Vite 7 编译 `.vue` 单文件组件。
### 停止
```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-vue2}
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.2/24 已被 go / rust 占用)
networks:
node-frontend-vue2-demo:
ipam:
driver: default
config:
- subnet: "172.31.3.0/24"
services:
frontend:
image: registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vue2 # 镜像由 build.sh 构建
container_name: node-frontend-vue2-demo
restart: unless-stopped
ports: # 映射端口:前端页面
- "8089: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-vue2-demo:
ipv4_address: 172.31.3.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-vue2 | Vue 2 + Element UI</title>
</head>
<body>
<div id="app"></div>
<script src="/src/main.js" type="module"></script>
</body>
</html>
@@ -0,0 +1,29 @@
# node-frontend-vue2 站点:Tengine 3.1.0 托管 Vue 2 构建产物(纯静态,无后端依赖)
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-vue2",
"version": "1.0.0",
"private": true,
"description": "Vue 2.7 + Element UI 2 前端示例,构建产物由 Tengine 托管",
"type": "module",
"engines": {
"node": ">=20.19"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview --port 4173"
},
"dependencies": {
"element-ui": "2.15.14",
"vue": "2.7.16",
"vue-router": "3.6.5"
},
"devDependencies": {
"@vitejs/plugin-vue2": "2.3.4",
"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-vue2-demo down
+23
View File
@@ -0,0 +1,23 @@
<template>
<div class="app">
<el-container>
<el-header height="56px" class="header">
<span class="logo">node-frontend-vue2</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>
</div>
</template>
<script>
export default {
name: 'App',
};
</script>
+16
View File
@@ -0,0 +1,16 @@
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './styles.css';
import App from './App.vue';
import router from './router';
// Vue 2.7 内置组合式 API,但示例保持 Vue 2 选项式写法,贴近 Element UI 2 的文档
Vue.use(ElementUI);
Vue.config.productionTip = false;
new Vue({
router,
render: (h) => h(App),
}).$mount('#app');
+18
View File
@@ -0,0 +1,18 @@
import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from './views/Home.vue';
Vue.use(VueRouter);
const routes = [
{ path: '/', name: 'home', component: Home },
{ path: '/form', name: 'form', component: () => import('./views/Form.vue') },
{ path: '/about', name: 'about', component: () => import('./views/About.vue') },
// 前端路由兜底:放在 Tengine 上时,需要 location / 里 try_files $uri $uri/ /index.html
{ path: '*', redirect: '/' },
];
export default new VueRouter({
mode: 'hash',
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 #ebeef5;
}
.logo {
font-size: 18px;
font-weight: 600;
color: #409eff;
}
@@ -0,0 +1,18 @@
<template>
<el-card shadow="never">
<div slot="header"><span>关于</span></div>
<el-descriptions :column="1" border>
<el-descriptions-item label="框架">Vue 2.7.16(选项式 API)</el-descriptions-item>
<el-descriptions-item label="组件库">Element UI 2.15.14</el-descriptions-item>
<el-descriptions-item label="构建">Vite 7 + @vitejs/plugin-vue2</el-descriptions-item>
<el-descriptions-item label="部署">Tengine 3.1.0(proxy-tengine3 镜像)托管 dist/</el-descriptions-item>
<el-descriptions-item label="路由模式">hash(无需服务端 rewrite;换成 history 需要 try_files 兜底)</el-descriptions-item>
</el-descriptions>
</el-card>
</template>
<script>
export default {
name: 'About',
};
</script>
@@ -0,0 +1,58 @@
<template>
<el-card shadow="never">
<div slot="header"><span>Element UI 2 常用组件</span></div>
<el-form ref="form" :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>
const emptyForm = () => ({ name: '', type: '', enabled: true, date: '' });
export default {
name: 'Form',
data() {
return {
form: emptyForm(),
result: '',
rules: {
name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
type: [{ required: true, message: '请选择类型', trigger: 'change' }],
},
};
},
methods: {
submit() {
this.$refs.form.validate((ok) => {
if (!ok) return;
this.result = `已提交:${JSON.stringify(this.form)}`;
this.$message.success('提交成功');
});
},
reset() {
this.$refs.form.resetFields();
this.result = '';
},
},
};
</script>
@@ -0,0 +1,109 @@
<template>
<div>
<el-row :gutter="16">
<el-col :span="8">
<el-card shadow="never">
<div slot="header"><span>运行时</span></div>
<el-descriptions :column="1" border>
<el-descriptions-item label="Vue">{{ vueVersion }}</el-descriptions-item>
<el-descriptions-item label="Element UI">{{ elementVersion }}</el-descriptions-item>
<el-descriptions-item label="路由模式">{{ routerMode }}</el-descriptions-item>
</el-descriptions>
</el-card>
</el-col>
<el-col :span="8">
<el-card shadow="never">
<div slot="header">
<span>计数器</span>
</div>
<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">
<div slot="header"><span>当前时间</span></div>
<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">
<div slot="header"><span>Element UI 2 组件一览</span></div>
<el-space wrap>
<el-button>默认</el-button>
<el-button type="primary">主要</el-button>
<el-button type="success">成功</el-button>
<el-button type="warning">警告</el-button>
<el-button type="danger">危险</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>
import Vue from 'vue';
import ElementUI from 'element-ui';
// 纯前端展示:所有数据都在浏览器里产生,不调用任何后端接口
export default {
name: 'Home',
data() {
return {
vueVersion: Vue.version,
elementVersion: ElementUI.version,
routerMode: this.$router.mode,
count: 0,
clock: this.format(new Date()),
enabled: true,
rate: 4,
percent: 60,
keyword: '',
timer: null,
};
},
created() {
this.timer = setInterval(() => {
this.clock = this.format(new Date());
}, 1000);
},
beforeDestroy() {
clearInterval(this.timer);
},
methods: {
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())}`;
},
},
};
</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-vue2-demo up -d
docker-compose -f docker-compose.yml -p node-frontend-vue2-demo ps
@@ -0,0 +1,26 @@
import { defineConfig } from 'vite';
import vue2 from '@vitejs/plugin-vue2';
// 纯静态前端:产物由 Tengine 托管,base 用相对路径,部署到任意子路径都能跑
export default defineConfig({
plugins: [vue2()],
base: './',
server: {
host: '0.0.0.0',
port: 5173,
},
build: {
outDir: 'dist',
// 不生成 sourcemap,减小部署体积;需要排查时改成 true
sourcemap: false,
// element-ui 体积较大,单独拆包,利用浏览器缓存
rollupOptions: {
output: {
manualChunks: {
'element-ui': ['element-ui'],
vue: ['vue', 'vue-router'],
},
},
},
},
});