- 添加MySQL数据库配置,包含主机地址、端口、用户名、密码和数据库名 - 创建独立的Koa服务器模块,包含路由配置和静态文件服务 - 实现MySQL连接池配置,支持命名占位符查询格式化 - 调整项目入口文件结构,分离MySQL和Koa服务模块 - 更新package.json配置,修改主入口文件并添加mysql2依赖
30 lines
669 B
JavaScript
30 lines
669 B
JavaScript
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}`);
|
|
});
|