koa模塊 koa-route 路由 route.get("路徑",路由函數) koa-static 靜態資源載入 const serve(路徑) koa-compose 中間件合成模塊 koa-body 提取表單post請求鍵值對,處理上傳文件 上下文context的response和reques ...
koa模塊
koa-route 路由 route.get("路徑",路由函數)
koa-static 靜態資源載入 const serve(路徑)
koa-compose 中間件合成模塊
koa-body 提取表單post請求鍵值對,處理上傳文件
上下文context的response和request
ctx.response.body //返回的主體內容
ctx.response.redirect //重定向
ctx.response.type //返回的MIME類型
ctx.response.path //獲取用戶請求的路徑
ctx.response.status //返回的狀態碼
ctx.request.accepts //請求的mime類型
ctx.request.method //請求的方法
ctx.request.url //請求的url
ctx.request.body //請求的body
中間件(middleware)
Logger 列印日誌 //不需要引入任何,直接輸入console.log,在命令框就會列印出來
中間件功能可以拆分成一個獨立函數比如叫logger,參數(ctx,next) 然後app.use(logger),用來載入中間件
基本上,Koa 所有的功能都是通過中間件實現的,前面例子裡面的main也是中間件。
每個中間件預設接受兩個參數,第一個參數是 Context 對象,第二個參數是next函數。
只要調用next函數,就可以把執行權轉交給下一個中間件。
中間件棧
多個中間件會形成一個棧結構(middle stack),以"先進後出"(first-in-last-out)的順序執行。
- 最外層的中間件首先執行。
- 調用
next
函數,把執行權交給下一個中間件。 - ...
- 最內層的中間件最後執行。
- 執行結束後,把執行權交回上一層的中間件。
- ...
- 最外層的中間件收回執行權之後,執行
next
函數後面的代碼。
例如:有多個中間件,每個中間件分別寫了next()函數,則每個中間件會分別先執行next()函數之前的列印,然後再分別執行next()之後的列印,如果不寫next()函數,那麼執行權就不會傳遞下去,則只列印第一個中間件的內容。
const one = (ctx, next) => { console.log('>> one'); next(); console.log('<< one'); } const two = (ctx, next) => { console.log('>> two'); next(); console.log('<< two'); } const three = (ctx, next) => { console.log('>> three'); next(); console.log('<< three'); } app.use(one); app.use(two); app.use(three);
>> one >> two >> three << three << two << one
const Koa = require('koa'); const app = new Koa(); const one = (ctx, next) => { console.log('>> one'); // next(); console.log('<< one'); } const two = (ctx, next) => { console.log('>> two'); // next(); console.log('<< two'); } const three = (ctx, next) => { console.log('>> three'); // next(); console.log('<< three'); } app.use(one); app.use(two); app.use(three); app.listen(3000);
>>one
<<one
const Koa = require('koa'); const app = new Koa(); const one = (ctx, next) => { console.log('>> one'); next(); console.log('<< one'); } const two = (ctx, next) => { console.log('>> two'); // next(); console.log('<< two'); } const three = (ctx, next) => { console.log('>> three'); next(); console.log('<< three'); } app.use(one); app.use(two); app.use(three); app.listen(3000);
>>one >>two <<two <<one
非同步中間件
比如讀取資料庫等的非同步操作,使用ES8 的 async和await
中間件的合成
koa-compose
模塊可以將多個中間件合成為一個
錯誤處理
ctx.throw()
方法 參數為錯誤的http狀態碼
當直接用ctx.throw()拋出錯誤之後,則不能在定義返回的頁面顯示內容,所以可以先設置返回的狀態碼為相應的狀態碼,然後定義返回頁面的內容
const main = ctx => { ctx.response.status = 404; ctx.response.body = 'Page Not Found'; };
處理錯誤的中間件
使用try..catch捕獲
try{
await next()
}catch{
錯誤處理
}
error 事件的監聽
運行過程中一旦出錯,Koa 會觸發一個error
事件。監聽這個事件,也可以處理錯誤。
釋放 error 事件
需要註意的是,如果錯誤被try...catch
捕獲,就不會觸發error
事件。這時,必須調用ctx.app.emit()
,手動釋放error
事件,才能讓監聽函數生效。
const handler = async (ctx, next) => { try { await next(); } catch (err) { ctx.response.status = err.statusCode || err.status || 500; ctx.response.type = 'html'; ctx.response.body = '<p>Something wrong, please contact administrator.</p>'; ctx.app.emit('error', err, ctx); } }; const main = ctx => { ctx.throw(500); }; app.on('error', function(err) { console.log('logging error ', err.message); console.log(err); });
Cookies
ctx.cookies
用來讀寫 Cookie。
表單
Web 應用離不開處理表單。本質上,表單就是 POST 方法發送到伺服器的鍵值對。koa-body模塊可以用來從 POST 請求的數據體裡面提取鍵值對。
文件上傳
參考鏈接:阮一峰老師的文章