路由
koa2 原生路由实现
简单例子
const Koa = require('koa');
const app = new Koa();
app.use(async ctx => {
let url = ctx.request.url;
ctx.body = url;
});
app.listen(3000);
定制化的路由
源码文件目录
├── index.js
├── package.json
└── view
├── 404.html
├── index.html
└── todo.html
demo 源码
const Koa = require('koa');
const fs = require('fs');
const app = new Koa();
/**
* 用Promise封装异步读取文件方法
* @param {string} page html文件名称
* @return {promise}
*/
function render(page) {
return new Promise((resolve, reject) => {
let viewUrl = `./view/${page}`;
fs.readFile(viewUrl, 'binary', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
/**
* 根据URL获取HTML内容
* @param {string} url koa2上下文的url,ctx.url
* @return {string} 获取HTML文件内容
*/
async function route(url) {
let view = '404.html';
switch (url) {
case '/':
view = 'index.html';
break;
case '/index':
view = 'index.html';
break;
case '/todo':
view = 'todo.html';
break;
case '/404':
view = '404.html';
break;
default:
break;
}
let html = await render(view);
return html;
}
app.use(async ctx => {
let url = ctx.request.url;
let html = await route(url);
ctx.body = html;
});
app.listen(3000);
console.log('[demo] route-simple is starting at port 3000');
koa-router 中间件
# koa2 对应的版本是 7.x
npm install --save koa-router@7
const Koa = require('koa');
const app = new Koa();
const Router = require('koa-router');
let home = new Router();
// 子路由1
home.get('/', async ctx => {
let html = `
<ul>
<li><a href="/page/helloworld">/page/helloworld</a></li>
<li><a href="/page/404">/page/404</a></li>
</ul>
`;
ctx.body = html;
});
// 子路由2
let page = new Router();
page
.get('/404', async ctx => {
ctx.body = '404 page!';
})
.get('/helloworld', async ctx => {
ctx.body = 'helloworld page!';
});
// 装载所有子路由
let router = new Router();
router.use('/', home.routes(), home.allowedMethods());
router.use('/page', page.routes(), page.allowedMethods());
// 加载路由中间件
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000, () => {
console.log('[demo] route-use-middleware is starting at port 3000');
});