**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)* - [learn koa2--controller & template](#learn-koa2--controller--template) - [controller](#controller) - [template](#template) ## learn koa2--controller & template ### controller 在关于路由的那一篇里,将路由的处理放在了`app/routes/`下的各个文件里,并在`app/routes/index.js`中引用全部进行处理。 但即便是这样,现有的方式也是不妥的。它把路由URL的判断和对应的处理混杂在了一起。因此,我们应该把路由的处理提取出来,作为额外的`controller`层: ```javascript // app/controllers/home.js const home = (ctx, next) => { ctx.body = 'this is home page'; }; const about = (ctx, next) => { ctx.body = 'this is about page'; }; export default { home, about }; ``` ```javascript // app/routes/home.js import koaRouter from 'koa-router'; import home from '../controllers/home'; const router = koaRouter({ prefix: '/' }); router.get('/', home.home); router.get('/about', home.about); module.exports = router; ``` That's All. Done. ### template 寻找了很多模板,最终决定使用`nunjucks`来做Koa2的模板: 1. `mozilla`出品,品质和维护起来有保障 2. 语法和文档都挺清晰 3. 类似于原生html,学习成本低 为什么不用[`pug`](https://github.com/pugjs/pug)? - 从事前端工作的我表示不是很喜欢`pug`的语法 ```bash $ npm install nunjucks --save # 为了能够让koa渲染 $ npm install koa-views --save ``` 为模板准备的目录结构: ```bash - app + controllers + routes - templates - home index.html about.html - layouts base.html app.js ``` 随便写下模板文件: ```html