feat(server): 重构服务端架构并集成MySQL数据库
- 添加MySQL数据库配置,包含主机地址、端口、用户名、密码和数据库名 - 创建独立的Koa服务器模块,包含路由配置和静态文件服务 - 实现MySQL连接池配置,支持命名占位符查询格式化 - 调整项目入口文件结构,分离MySQL和Koa服务模块 - 更新package.json配置,修改主入口文件并添加mysql2依赖
This commit is contained in:
0
module/server/.env
Normal file
0
module/server/.env
Normal file
@@ -1,3 +1,9 @@
|
|||||||
export default {
|
export default {
|
||||||
|
mysql: {
|
||||||
|
host: '192.168.25.110',
|
||||||
|
port: 3307,
|
||||||
|
user: 'root',
|
||||||
|
password: 'mysql_Adkijc',
|
||||||
|
database: 'mir_web'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +1,2 @@
|
|||||||
import Koa from 'koa';
|
import "./mysql/index.js"
|
||||||
import Router from 'koa-router';
|
import "./koa/index.js"
|
||||||
import config from "./config/index.js"
|
|
||||||
import koaStatic from 'koa-static';
|
|
||||||
|
|
||||||
const app = new Koa();
|
|
||||||
const router = new Router();
|
|
||||||
|
|
||||||
// 简单的路由示例
|
|
||||||
router.get('/', (ctx) => {
|
|
||||||
ctx.body = {message: 'Hello from Koa server!'};
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/api/test', (ctx) => {
|
|
||||||
ctx.body = {message: 'This is a test API endpoint'};
|
|
||||||
});
|
|
||||||
|
|
||||||
router.get('/api/config', (ctx) => {
|
|
||||||
ctx.body = {data: config}
|
|
||||||
})
|
|
||||||
app.use(router.routes());
|
|
||||||
app.use(router.allowedMethods());
|
|
||||||
app.use(koaStatic('/www'))
|
|
||||||
|
|
||||||
const PORT = process.env.PORT || 3001;
|
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
|
||||||
console.log(`Koa server is running on port ${PORT}`);
|
|
||||||
});
|
|
||||||
|
|||||||
29
module/server/koa/index.js
Normal file
29
module/server/koa/index.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import Koa from 'koa';
|
||||||
|
import Router from 'koa-router';
|
||||||
|
import config from "../config/index.js"
|
||||||
|
import koaStatic from 'koa-static';
|
||||||
|
|
||||||
|
const app = new Koa();
|
||||||
|
const router = new Router();
|
||||||
|
|
||||||
|
// 简单的路由示例
|
||||||
|
router.get('/', (ctx) => {
|
||||||
|
ctx.body = {message: 'Hello from Koa server!'};
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/api/test', (ctx) => {
|
||||||
|
ctx.body = {message: 'This is a test API endpoint'};
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/api/config', (ctx) => {
|
||||||
|
ctx.body = {data: config}
|
||||||
|
})
|
||||||
|
app.use(router.routes());
|
||||||
|
app.use(router.allowedMethods());
|
||||||
|
app.use(koaStatic('/www'))
|
||||||
|
|
||||||
|
const PORT = process.env.PORT || 3001;
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
console.log(`Koa server is running on port ${PORT}`);
|
||||||
|
});
|
||||||
18
module/server/mysql/index.js
Normal file
18
module/server/mysql/index.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import mysql from "mysql2";
|
||||||
|
import config from "../config/index.js";
|
||||||
|
|
||||||
|
const pool = mysql.createPool({
|
||||||
|
...config.mysql,
|
||||||
|
queryFormat: function (sql, values) {
|
||||||
|
const opts = { sql, values }
|
||||||
|
this._resolveNamedPlaceholders(opts)
|
||||||
|
return mysql2.format(
|
||||||
|
opts.sql,
|
||||||
|
opts.values,
|
||||||
|
this.config.stringifyObjects,
|
||||||
|
this.config.timezone
|
||||||
|
)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default pool.promise();
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "chuanqi-server",
|
"name": "chuanqi-server",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "A simple Koa server for chuanqi web",
|
"description": "A simple Koa server for chuanqi web",
|
||||||
"main": "index.js",
|
"main": "koa/index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js",
|
"start": "node index.js",
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"koa": "^2.15.0",
|
"koa": "^2.15.0",
|
||||||
"koa-router": "^12.0.0",
|
"koa-router": "^12.0.0",
|
||||||
"koa-static": "^5.0.0"
|
"koa-static": "^5.0.0",
|
||||||
|
"mysql2": "^3.16.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user