前端架构有哪些常见模式?微前端、低代码、BFF 是什么?
前端架构演进
单体应用 → 组件化 → 模块化 → 微前端 → 低代码1. BFF(Backend for Frontend)
为前端定制的后端层,聚合多个微服务的数据。
前端 → BFF → 微服务A、微服务B、微服务C
↑
数据聚合、格式转换、缓存优势:
- 前端只需一次请求获取所有数据
- 避免前端直接依赖多个服务
- 可以做数据裁剪和格式转换
技术栈: Node.js (Nest.js / Express)、GraphQL
// BFF 示例
app.get('/dashboard', async (req, res) => {
const [user, orders, notifications] = await Promise.all([
userService.getUser(req.userId),
orderService.getOrders(req.userId),
notificationService.getNotifications(req.userId)
])
res.json({ user, orders, notifications })
})2. Monorepo(单仓库多项目)
my-project/
├── packages/
│ ├── ui/ # 共享 UI 组件库
│ ├── utils/ # 共享工具函数
│ ├── types/ # 共享类型定义
│ ├── web/ # Web 应用
│ ├── mobile/ # 移动应用
│ └── admin/ # 管理后台
├── package.json
└── turbo.json # Turborepo 配置工具: Turborepo、Nx、pnpm workspaces
3. 低代码(Low-Code)
通过可视化拖拽生成页面,减少手写代码。
核心能力:
- 可视化编辑器
- 组件物料库
- 属性配置面板
- 代码生成/运行时渲染
- 数据源绑定
// 页面 Schema
{
"type": "page",
"children": [
{
"type": "form",
"props": { "title": "用户信息" },
"children": [
{ "type": "input", "props": { "label": "姓名", "field": "name" } },
{ "type": "select", "props": { "label": "角色", "field": "role", "options": [...] } }
]
}
]
}
// 渲染器
function renderSchema(node) {
const Component = componentMap[node.type]
return (
<Component {...node.props}>
{node.children?.map(renderSchema)}
</Component>
)
}4. 微前端
(详见 FW-035)
5. 组件库设计
// 设计系统
├── tokens/ # 设计变量(颜色、间距、字体)
├── primitives/ # 原子组件(Button、Input)
├── components/ # 业务组件(Form、Table)
├── templates/ # 页面模板
└── theme/ # 主题配置Headless UI 趋势:
// 逻辑与 UI 分离
// Headless Hooks(如 @tanstack/react-table)
const table = useTable({ columns, data })
// table.getRowModel() 等纯逻辑 API
// 你自己写 UI架构选择
| 场景 | 推荐架构 |
|---|---|
| 小型项目 | 组件化 + Vite |
| 中型项目 | 模块化 + 状态管理 |
| 大型项目 | Monorepo + 微前端 |
| 中后台 | 低代码平台 |
| 多端统一 | Taro / uni-app / React Native |
前端趋势
- AI 辅助开发:AI 生成代码、组件
- 边缘计算:Edge Functions(Vercel/Cloudflare Workers)
- Server Components:React RSC
- WebAssembly:高性能前端应用
- 跨端统一:Flutter / Taro / uni-app