今天開始學習目前呼聲很高的koa模塊,不過目前使用koa還是要用
--harmony-generator
標識,即便在剛出的nodejs 12上也是如此~,io.js倒是不需要,去年的nodejs和iojs分派不知道會不會帶來什么大的變革,不過既然koa目前呼聲這么高,肯定有他值得學習的地方,所以先搞着吧,多學點總比不學好。
這篇文章把koa依賴的模塊列舉一下,以后再自己學習koa的過程中減少一點查詢的過程,也算是nodejs打基礎的第一步吧~
accepts
Higher level content negotiation based on negotiator. Extracted from koa for general use. In addition to negotiator, it allows:
- Allows types as an array or arguments list, ie (['text/html', 'application/json']) as well as ('text/html', 'application/json').
- Allows type shorthands such as json.
- Returns false when no types match
- Treats non-existent headers as *
一個比較高級的API用來http處理請求中的接受類型,比如: 判斷請求可接收的類型
這個本身包含在koa里面的,現在是提取出來可以作為其他用途
https://www.npmjs.com/package/accepts
co
Generator based control flow goodness for nodejs and the browser, using promises, letting you write non-blocking code in a nice-ish way.
基於generator語法的控制流,koa的generator就是來自這個?
https://www.npmjs.com/package/co
Composition
Compose generator and async/await middleware like koa. Allows to use both generator functions and async/await functions.
撰寫跟koa風格一致的代碼,koa包含這個。。。傻傻分不清楚
https://www.npmjs.com/package/composition
Content-Disposition
Create and parse HTTP Content-Disposition header
貌似用於文件下載的時候自動適配文件的類型把類型寫入Content-Type
https://www.npmjs.com/package/content-disposition
2015-2-27保存草稿...明天繼續
content-type
Create and parse HTTP Content-Type header according to RFC 7231
用來處理Content-Type
頭信息的
https://www.npmjs.com/package/content-type
cookie
cookie is a basic cookie parser and serializer. It doesn't make assumptions about how you are going to deal with your cookies. It basically just provides a way to read and write the HTTP cookie headers.
See RFC6265 for details about the http header for cookies.
只是提供一個讀寫cookie的API
https://www.npmjs.com/package/cookie
debug
tiny node.js debugging utility modelled after node core's debugging technique.
貌似就是一個封裝的打印log的小東西?待研究~
https://www.npmjs.com/package/debug
delegates
Node method and accessor delegation utilty.
代理?不明,待研究
https://www.npmjs.com/package/delegates
destroy
Destroy a stream.
用來摧毀一個strem對象
https://www.npmjs.com/package/destroy
error-inject
inject an error listener into a stream
給stream對象注入一個錯誤監聽
https://www.npmjs.com/package/error-inject
escape-html
Escape HTML entities
這個。。。顧名思義了,跟前端的escape一個意思
https://www.npmjs.com/package/escape-html
fresh
HTTP response freshness testing
貌似是一個根據Etag來判斷是否cache的東西,如果是cache的就直接返回304
https://www.npmjs.com/package/fresh
http-assert
Assert with status codes. Like ctx.throw() in Koa, but with a guard.
通過狀態碼進行斷言?猜測是用來判斷請求狀態的,全部代碼只有33行...
https://www.npmjs.com/package/http-assert
http-errors
Create HTTP errors for Express, Koa, Connect, etc. with ease.
又是一個從koa里面分離出去的模塊,主要作用是用來簡單得創建http錯誤信息
https://www.npmjs.com/package/http-errors
mime-types
The ultimate javascript content-type utility.
Similar to node-mime, except:
- No fallbacks. Instead of naively returning the first available type, mime-types simply returns false, so do var type = mime.lookup('unrecognized') || 'application/octet-stream'.
- No new Mime() business, so you could do var lookup = require('mime-types').lookup.
- Additional mime types are added such as jade and stylus via mime-db
- No .define() functionality
Otherwise, the API is compatible.
最終版的MIME幫助API,重新定義了API實現,更加方便,也更加健全
https://www.npmjs.com/package/mime-types
on-finished
Execute a callback when a request closes, finishes, or errors
在請求結束的時候調用一個回調
https://www.npmjs.com/package/on-finished
only
return whitelisted properties of an object
返回白名單中的值。其實就是根據一個列表獲取一個對象中的部分屬性返回一個obj
https://www.npmjs.com/package/only
parseurl
parse a url with memoization
返回req.url,返回結果和url.parse一樣(不知道這個url是不是req.url),多次對同一個req調用該方法會直接返回緩存的結果,不在進行parse
https://www.npmjs.com/package/parseurl
statuses
HTTP status utility for node.
http狀態工具status(403) // => 'Forbidden'
,可以通過code轉換成msg,也可以通過msg轉換成code
https://www.npmjs.com/package/statuses
type-is
Infer the content-type of a request.
判斷一個請求的content-type
https://www.npmjs.com/package/type-is
vary
Manipulate the HTTP Vary header
推斷不同的http頭的內容?貌似可以根據頭來判斷值
https://www.npmjs.com/package/vary
好了,基本把所有的都列出來了,在這個過程中發現有很多的都是從koa中分離出來的,所有大體上可以認為koa基本就是從頭開始的(貌似是屁話)。還有兩個沒列,但是也沒必要了,名字如下:
- koa-compose
- koa-is-json
你們懂的