Egg.js+Antd打造极简阿里云CS控制台
2023-10-20 18:56:50
**一、前言**
阿里云CS(Container Service)是一个云原生的容器服务平台,它提供了包括容器编排、服务发现、负载均衡、弹性伸缩等功能。本文将介绍如何使用Egg.js和Antd构建一个简单的阿里云CS控制台。我们将重点介绍如何使用Egg.js搭建后端服务,以及如何使用Antd构建前端界面。
**二、准备工作**
在开始之前,我们需要准备以下软件:
* Node.js v8.x或更高版本
* npm v6.x或更高版本
* Egg.js v2.x或更高版本
* Antd v4.x或更高版本
**三、搭建后端服务**
1. 创建Egg.js项目
mkdir my-cs-console
cd my-cs-console
npm init egg --type=simple
2. 安装必要的依赖
npm install --save egg-alinode egg-cors egg-mongoose egg-router egg-view-ejs
3. 配置Egg.js
在`config/config.default.js`中,配置数据库和路由:
// config/config.default.js
exports.mongoose = {
client: {
url: 'mongodb://localhost/my-cs-console',
options: {
useNewUrlParser: true,
useUnifiedTopology: true
}
}
};
exports.alinode = {
appid: 'your-appid',
secret: 'your-secret'
};
exports.cors = {
origin: 'http://localhost:3000',
credentials: true
};
exports.router = {
prefix: '/api'
};
exports.view = {
defaultViewEngine: 'ejs'
};
4. 创建控制器
在`app/controller/home.js`中,创建HomeController:
// app/controller/home.js
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
const { ctx } = this;
const clusters = await ctx.service.cluster.list();
await ctx.render('home/index.ejs', { clusters });
}
}
module.exports = HomeController;
5. 创建服务
在`app/service/cluster.js`中,创建ClusterService:
// app/service/cluster.js
'use strict';
const Service = require('egg').Service;
class ClusterService extends Service {
async list() {
const { ctx } = this;
const clusters = await ctx.model.Cluster.find();
return clusters;
}
}
module.exports = ClusterService;
6. 创建模型
在`app/model/cluster.js`中,创建Cluster模型:
// app/model/cluster.js
'use strict';
const mongoose = require('mongoose');
const ClusterSchema = new mongoose.Schema({
name: { type: String, required: true },
region: { type: String, required: true },
zone: { type: String, required: true },
status: { type: String, required: true },
created_at: { type: Date, default: Date.now }
});
const Cluster = mongoose.model('Cluster', ClusterSchema);
module.exports = Cluster;
7. 启动Egg.js
npm start
**四、构建前端界面**
1. 创建React项目
npx create-react-app my-cs-console-ui
2. 安装必要的依赖
npm install --save antd @ant-design/icons react-router-dom
3. 配置React项目
在`src/index.js`中,配置路由:
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './pages/Home';
ReactDOM.render(
document.getElementById('root')
);
4. 创建页面
在`src/pages/Home.js`中,创建HomePage:
// src/pages/Home.js
import React, { useEffect, useState } from 'react';
import { Table, Button, Space } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
const Home = () => {
const [clusters, setClusters] = useState([]);
useEffect(() => {
fetch('/api/clusters')
.then(res => res.json())
.then(data => setClusters(data))
.catch(err => console.error(err));
}, []);
const columns = [
{ title: '名称', dataIndex: 'name', key: 'name' },
{ title: '地域', dataIndex: 'region', key: 'region' },
{ title: '可用区', dataIndex: 'zone', key: 'zone' },
{ title: '状态', dataIndex: 'status', key: 'status' },
{
title: '操作',
key: 'action',
render: () => (
)
}
];
return (
<Table columns={columns} dataSource={clusters} rowKey="_id" />
<Button type="primary" icon={
);
};
export default Home;
5. 启动React项目
npm start
**五、结语**
本文介绍了如何使用Egg.js和Antd构建一个简单的阿里云CS控制台。我们重点介绍了如何使用Egg.js搭建后端服务,以及如何使用Antd构建前端界面。希望本文对你有帮助。