koa2 源码解读

简介
使用koa 编写web 应用,通过组合不同的generator,可免除重复繁琐的回调函数嵌套,并极大地提升错误处理的效率。koa 不在内核方法中绑定任何中间键,它仅仅提供一个优雅的函数库,使得编写Web 应用变得得心应手。

源文件结构
这里写图片描述
application.js 是整个koa2 的文件入口,封装了context, request, response, 以及最核心的中间键处理流程。
context.js 处理应用上下文,里面直接封装部分request.js 和response.js 的方法
request.js 处理http 请求
response.js 处理http 响应


Koa Express 都是node.js 的Web 服务框架
核心服务:
HTTP accept parse response(return web page or JSON text)
middleware (In the middle of parsing requests and response requests — will have third part middleware)
context(执行上下文,http request 周期内的环境,托管请求响应和中间键,方便他们访问)
对应koa2
application context request response middleware cookie session

HTTP
To use the HTTP server and client one must require(‘http’)
The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally difficult to use.
http.createServer([options][,requestLister])
Returns a new instance of http.Server
这里写图片描述

猜你喜欢

转载自blog.csdn.net/yana_loo/article/details/80669157