在上一节,我们新增路由管理模块,新建一个 urls 目录 存放控制器,然后这些控制器通过 app.js 的 koa-router 模块加载。
美中不足的是 首页的 url 处理放在了 app.js,而首页往往含有比较多的处理逻辑,这样会使得 app.js 很臃肿了。
于是,我参照廖雪峰老师的思路,来进行路由模块管理,下面给出最终代码:
一、/app.js
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const controller = require('./controller');
const app = new Koa();
// 控制台请求URL日志
app.use(async (ctx, next) => {
console.log(`请求 ${ctx.request.method} ${ctx.request.url}...`);
await next();
});
// 解析 request(post) body:
app.use(bodyParser());
// 添加 controllers:
app.use(controller());
app.listen('3000');
console.log('应用在3000端口启动...');
二、/controller.js
const fs = require('fs');
function addMapping(router, mapping) {
for (var url in mapping) {
if (url.startsWith('GET ')) {
var path = url.substring(4);
router.get(path, mapping[url]);
console.log(`注册路由: GET ${path}`);
} else if (url.startsWith('POST ')) {
var path = url.substring(5);
router.post(path, mapping[url]);
console.log(`注册路由: POST ${path}`);
} else {
console.log(`无效的URL: ${url}`);
}
}
}
function addControllers(router, dir) {
// 用sync是因为启动时只运行一次,不存在性能问题
fs.readdirSync(__dirname + '/' + dir).filter((f) => {
// 过滤出.js文件
return f.endsWith('.js');
}).forEach((f) => {
console.log(`加载文件: ${f}...`);
// 导入js文件
let mapping = require(__dirname + '/' + dir + '/' + f);
addMapping(router, mapping);
});
}
module.exports = function (dir) {
let controllers_dir = dir || 'controllers'; // 如果不传参数,扫描目录默认为'controllers'
let router = require('koa-router')();
addControllers(router, controllers_dir);
return router.routes();
};
三、/controllers/index.js
var fn_index = async (ctx, next) => {
ctx.response.body = `<h1>Index</h1>
<form action="/signin" method="post">
<p>Name: <input name="name" value="koa"></p>
<p>Password: <input name="password" type="password"></p>
<p><input type="submit" value="Submit"></p>
</form>`;
};
var fn_signin = async (ctx, next) => {
var
name = ctx.request.body.name || '',
password = ctx.request.body.password || '';
console.log(`signin with name: ${name}, password: ${password}`);
if (name === 'koa' && password === '12345') {
ctx.response.body = `<h1>Welcome, ${name}!</h1>`;
} else {
ctx.response.body = `<h1>Login failed!</h1>
<p><a href="/">Try again</a></p>`;
}
};
module.exports = {
'GET /': fn_index,
'POST /signin': fn_signin
};
四、/controllers/hello.js
var fn_hello = async (ctx, next) => {
var name = ctx.params.name;
ctx.response.body = `<h1>Hello, ${name}!</h1>`;
};
module.exports = {
'GET /hello/:name': fn_hello
};
五、控制台结果
[Running] node "e:\xushanxiang\koaproj\app.js"
加载文件: hello.js...
注册路由: GET /hello/:name
加载文件: index.js...
注册路由: GET /
注册路由: POST /signin
应用在3000端口启动...
请求 GET /...
请求 POST /signin...
...