feat(api): 实现 API 路由自动加载并优化路由处理
- 新增 api/example.js 文件作为示例 POST API - 在 app.js 中添加(bodyParser) 中间件 - 实现 loadAPIRoutes 函数自动加载 API 路由 - 更新 JWT 中间件配置,保护 /login 路由 - 优化路由定义,使用箭头函数
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
# 环境变量文件
|
# 环境变量文件
|
||||||
.env
|
.env
|
||||||
node_modules/
|
node_modules/
|
||||||
|
package-lock.json
|
||||||
|
|||||||
7
api/example.js
Normal file
7
api/example.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
// api/example.js
|
||||||
|
module.exports = (ctx) => {
|
||||||
|
ctx.body = {
|
||||||
|
message: 'Example POST API',
|
||||||
|
data: ctx.request.body
|
||||||
|
};
|
||||||
|
};
|
||||||
63
app.js
63
app.js
@@ -1,34 +1,59 @@
|
|||||||
require('dotenv').config(); // 在文件最顶部加载环境变量
|
require("dotenv").config();
|
||||||
|
const Koa = require("koa");
|
||||||
const Koa = require('koa');
|
const Router = require("koa-router");
|
||||||
const Router = require('koa-router');
|
const jwt = require("jsonwebtoken");
|
||||||
const jwt = require('jsonwebtoken');
|
const koaJwt = require("koa-jwt");
|
||||||
const koaJwt = require('koa-jwt');
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
const bodyParser = require("koa-bodyparser");
|
||||||
|
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
|
app.use(bodyParser()); // 添加在路由中间件之前
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
|
// 自动加载API路由函数
|
||||||
|
const loadAPIRoutes = () => {
|
||||||
|
const apiDir = path.join(__dirname, "api");
|
||||||
|
const files = fs.readdirSync(apiDir);
|
||||||
|
|
||||||
|
files.forEach((file) => {
|
||||||
|
if (file.endsWith(".js") && file !== "index.js") {
|
||||||
|
const routePath = `/${file.replace(".js", "")}`;
|
||||||
|
const handler = require(path.join(apiDir, file));
|
||||||
|
|
||||||
|
router.post(routePath, async (ctx) => {
|
||||||
|
await handler(ctx);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// 公开路由
|
// 公开路由
|
||||||
router.get('/public', ctx => {
|
router.get("/public", (ctx) => {
|
||||||
ctx.body = 'Public content';
|
ctx.body = "Public content";
|
||||||
});
|
});
|
||||||
|
|
||||||
// 登录路由
|
// 登录路由
|
||||||
router.post('/login', ctx => {
|
router.post("/login", (ctx) => {
|
||||||
const user = { id: 1, username: 'admin' };
|
const user = { id: 1, username: "admin" };
|
||||||
const token = jwt.sign(user, process.env.JWT_SECRET, { expiresIn: '1h' });
|
const token = jwt.sign(user, process.env.JWT_SECRET, { expiresIn: "1h" });
|
||||||
ctx.body = { token };
|
ctx.body = { token };
|
||||||
});
|
});
|
||||||
|
|
||||||
// JWT中间件
|
// JWT中间件(保护下方所有路由)
|
||||||
app.use(koaJwt({
|
app.use(
|
||||||
secret: process.env.JWT_SECRET
|
koaJwt({
|
||||||
}).unless({
|
secret: process.env.JWT_SECRET,
|
||||||
path: [/^\/public/]
|
}).unless({
|
||||||
}));
|
path: [/^\/public/, /^\/login/],
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// 加载自动生成的路由
|
||||||
|
loadAPIRoutes();
|
||||||
|
|
||||||
// 受保护路由
|
// 受保护路由
|
||||||
router.get('/protected', ctx => {
|
router.get("/protected", (ctx) => {
|
||||||
ctx.body = `Protected content for ${ctx.state.user.username}`;
|
ctx.body = `Protected content for ${ctx.state.user.username}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -37,4 +62,4 @@ app.use(router.allowedMethods());
|
|||||||
|
|
||||||
app.listen(process.env.PORT || 3000, () => {
|
app.listen(process.env.PORT || 3000, () => {
|
||||||
console.log(`Server running on http://localhost:${process.env.PORT || 3000}`);
|
console.log(`Server running on http://localhost:${process.env.PORT || 3000}`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"koa": "^2.15.4",
|
"koa": "^2.15.4",
|
||||||
|
"koa-bodyparser": "^4.4.1",
|
||||||
"koa-jwt": "^4.0.4",
|
"koa-jwt": "^4.0.4",
|
||||||
"koa-router": "^13.0.1"
|
"koa-router": "^13.0.1"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user