初始化实例代理上下文context 实现
狭义中间件区别于请求/响应拦截的另一种方式是上下文context代理。
上下文context代理分成两种
这里先将第一种代理方式——实例代理上下文context实现步骤,实例代理的比较有代表性的中间件是官方提 koa-ejs
中间件,把渲染的方法挂载在Koa
实例app
的app.context
属性中。
常见化实例代理上下文context实现步骤
Koa
实例 let app = new Koa()
demo
挂载在 app.context
上,app.context.demo
app.use()
中间件直接使用 ctx.demo
方法或属性这里我们实现最简单的模板渲染中间件 view
,模仿koa-ejs
的基本能力。
view
的实现步骤
Koa
实例 let app = new Koa()
view
挂载在 app.context
上,app.context.view
app.use()
中间件直接使用 ctx.view
方法或属性渲染模板demo源码
https://github.com/chenshenhai/koajs-design-note/tree/master/demo/chapter-05-01
## 安装依赖npm i## 执行 demonpm run start## 最后启动chrome浏览器访问## http://127.0.0.1:3000/index## http://127.0.0.1:3000/hello
const path = require('path');const fs = require('fs');function view(app, opts = {}) {const {baseDir = ''} = opts;// 将需要的属性或者方法 `view` 挂载在 `app.context` 上,`app.context.view`app.context.view = function(page = '', obj = {}) {let ctx = this;let filePath = path.join(baseDir, page);if (fs.existsSync(filePath)) {let tpl = fs.readFileSync(filePath, 'binary');ctx.body = tpl;} else {ctx.throw(404);}};}module.exports = view;
.├── example.js├── index.js└── views├── hello.html└── index.html
// example.jsconst Koa = require('koa');const path = require('path');const view = require('./index');// 初始化一个`Koa`实例 `let app = new Koa()`const app = new Koa();// 将需要的属性或者方法 `view` 挂载在 `app.context` 上,`app.context.view`view(app, {baseDir: path.join(__dirname, 'views')});app.use(async ctx => {await ctx.view(`${ctx.path}.html`, {title: 'index page'});});app.listen(3000, () => {console.log('[demo] jsonp is starting at port 3000');});