use(fn) {
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
if (isGeneratorFunction(fn)) {
deprecate('Support for generators will be removed in v3. ' +
'See the documentation for examples of how to convert old middleware ' +
'https://github.com/koajs/koa/blob/master/docs/migration.md');
fn = convert(fn);
}
debug('use %s', fn._name || fn.name || '-');
this.middleware.push(fn);
return this;
}
function compose (middleware) {
if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!')
for (const fn of middleware) {
if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!')
}
/**
@param {Object} context
@return {Promise}
@api public
*/
return function (context, next) {
// last called middleware #
let index = -1
return dispatch(0)
function dispatch (i) {
if (i <= index) return Promise.reject(new Error('next() called multiple times'))
index = i
let fn = middleware[i]
if (i === middleware.length) fn = next
if (!fn) return Promise.resolve()
try {
return Promise.resolve(fn(context, function next () {
return dispatch(i + 1)
}))
} catch (err) {
return Promise.reject(err)
}
}
}
}
第一步
原生nodejs创建http服务器,监听端口3000,触发handleRequest函数。对于koa来说,同样是监听端口,在实际运用中,handleRequest会包含大量的逻辑代码,因此要进行拆分成多个函数。 koa的实现就是对这个“拆分”的实现。handleRequest拆分成了多个中间件函数,通过next作为控制开关,是否流转到下一个中间件。
koa实现一个简单的服务器
}
const Koa = require('koa') const app = new Koa()
app .use(async (ctx, next) => { console.log(1) await next() console.log(5) }) .use(async (ctx, next) => { console.log(2) await next() console.log(4) }) .use(async (ctx, next) => { ctx.body = 'hello world' console.log(3) })
app.listen(3000);
function compose (middleware) { if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!') for (const fn of middleware) { if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!') }
/**
@api public */
return function (context, next) { // last called middleware # let index = -1 return dispatch(0) function dispatch (i) { if (i <= index) return Promise.reject(new Error('next() called multiple times')) index = i let fn = middleware[i] if (i === middleware.length) fn = next if (!fn) return Promise.resolve() try { return Promise.resolve(fn(context, function next () { return dispatch(i + 1) })) } catch (err) { return Promise.reject(err) } } } }