Node 主要用在開發 Web 應用,koa 是目前 node 裏最流行的 web 框架。前端
在 Node 開啓一個 http 服務簡直易如反掌,官網 demo。node
const http = require("http"); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader("Content-Type", "text/plain"); res.end("Hello World\n"); }); const hostname = "127.0.0.1"; const port = 3000; server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); }); //前端全棧學習交流圈:866109386 //面向1-3經驗年前端開發人員 //幫助突破技術瓶頸,提高思惟能力
koa 應用web
koa 如何建立一個 server, 直接上個官網的例子編程
const Koa = require("koa"); const app = new Koa(); // x-response-time app.use(async (ctx, next) => { const start = Date.now(); await next(); const ms = Date.now() - start; ctx.set("X-Response-Time", `${ms}ms`); }); // logger app.use(async (ctx, next) => { const start = Date.now(); await next(); const ms = Date.now() - start; console.log(`${ctx.method} ${ctx.url} - ${ms}`); }); // response app.use(async ctx => { ctx.body = "Hello World"; }); app.listen(3000); //前端全棧學習交流圈:866109386 //面向1-3經驗年前端開發人員 //幫助突破技術瓶頸,提高思惟能力
中間件概念在編程中使用普遍, 無論是前端仍是後端, 在實際編程中或者框架設計都有使用到這種實用的模型。後端
基本上,Koa 全部的功能都是經過中間件實現的。app
每一箇中間件默認接受兩個參數,第一個參數是 Context 對象,第二個參數是 next 函數。只要調用 next 函數,就能夠把執行權轉交給下一個中間件。框架
若是中間件內部沒有調用 next 函數,那麼執行權就不會傳遞下去。koa
多箇中間件會造成一個棧結構(middle stack),以「先進後出」(first-in-last-out)的順序執行。整個過程就像,先是入棧,而後出棧的操做。async
上面代碼的執行順序是:函數
請求 ==> x-response-time 中間件 ==> logger 中間件 ==> response中間件 ==> logger 中間件 ==> response-time 中間件 ==> 響應
理解 Koa 的中間件機制(源碼分析)
閱讀源碼,化繁爲簡,咱們看看 koa 的中間件系統是如何實現的。
function compose(middleware) { 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, dispatch.bind(null, i + 1))); } catch (err) { return Promise.reject(err); } } }; } //前端全棧學習交流圈:866109386 //面向1-3經驗年前端開發人員 //幫助突破技術瓶頸,提高思惟能力
我試圖去簡化一下這個方法,但方法自己已經足夠簡潔。
代碼很簡潔。
經過 next()傳遞 實現中間件調用, 結合 Promise 採用 遞歸調用 的通知機制。
看圖 這種形式的控制流讓整個 Koa 框架中間件的訪問呈現出 自上而下的中間件流 + 自下而上的 response 數據流 的形式。
Koa 自己作的工做僅僅是定製了中間件的編寫規範,而不內置任何中間件。一個 web request 會經過 Koa 的中間件棧,來動態完成 response 的處理。
koa 在中間件語法上面採用了 async + await 語法來生成 Promise 形式的程序控制流。