广义中间件,间接中间件方式实现,还有一个官方的中间件 koa-mount
,让多个Koa.js子应用合并成一个父应用,用请求的前缀区分子应用。这里基于第三方中间件 koa-mount
用最简单的方式实现 koa-mount
最简单功能。
https://github.com/chenshenhai/koajs-design-note/tree/master/demo/chapter-06-02
## 安装依赖npm i## 执行 demonpm run start## 最后启动chrome浏览器访问## http://127.0.0.1:3000/app1## http://127.0.0.1:3000/app1
koa-compose
const path = require('path');const compose = require('./compose');function mount(prefix, app) {let middleware = app.middleware;let middlewareStream = compose(middleware || []);if( prefix === '/' ) {return middlewareStream;}return async function( ctx, next ) {let mountPath = matchPath(ctx.path);if( !mountPath ) {return await next();}let originPath = ctx.path;ctx.path = mountPath;await middlewareStream(ctx, async () => {ctx.path = originPath;await next();ctx.path = mountPath});ctx.path = originPath;}function matchPath( originPath ) {if( originPath.indexOf(prefix) < 0 ) {return false;}const mountPath = originPath.replace(prefix, '') || '/';if( mountPath[0] !== '/' ) {return false;}return mountPath;}}module.exports = mount;
const mount = require('./index');const Koa = require('koa');const app1 = new Koa();const app2 = new Koa();app1.use(async (ctx, next) => {await next()ctx.body = 'app 1'})app2.use(async (ctx, next) => {await next()ctx.body = 'app 2'})const app = new Koa()app.use(mount('/app1', app1))app.use(mount('/app2', app2))app.listen(3000)console.log('listening on port 3000');