node中间件KOA函数
生活随笔
收集整理的這篇文章主要介紹了
node中间件KOA函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
const Koa = require('koa');
const app = new Koa()
//應用程序對象 中間件
// 發送HTTP KOA 接手HTTP
//中間件(其實就是 函數)
function test(){ console.log("seven month"); } //當請求發送過來的時候,將函數(中間件)注冊到程序上 //前端發送一個http請求 來觸發中間件 //koa 中 只會執行第一個中間件
app.use(async(ctx, next)=>{ //ctx 上下文 // next 下一個中間函數 console.log("7") const a = await next() //調用下一個中間件 // await 可以將promise 中對象 值 直接獲取(不通過then) // await 對表達式求值 //await 阻塞線程(異步) (將異步變為同步) console.log(a) // a.then((res)=>{ // console.log(res) // }) console.log("8") }) // 洋蔥模型(如果有await, 則需要在每個中間件函數前面加async next前面加上 await, ha)
app.use(async(ctx, next) =>{ console.log("9")
const res = await axios.get("http://7yue.pro") next()
// 對資源的操作 都是異步的 讀文件 發送http請求 操作數據庫
console.log('10') return "seven" })
// 打印結果為7 9 10 8
// 中間件(函數) return 會被強制轉換成一個 Promise app.listen(3000);
const app = new Koa()
//應用程序對象 中間件
// 發送HTTP KOA 接手HTTP
//中間件(其實就是 函數)
function test(){ console.log("seven month"); } //當請求發送過來的時候,將函數(中間件)注冊到程序上 //前端發送一個http請求 來觸發中間件 //koa 中 只會執行第一個中間件
app.use(async(ctx, next)=>{ //ctx 上下文 // next 下一個中間函數 console.log("7") const a = await next() //調用下一個中間件 // await 可以將promise 中對象 值 直接獲取(不通過then) // await 對表達式求值 //await 阻塞線程(異步) (將異步變為同步) console.log(a) // a.then((res)=>{ // console.log(res) // }) console.log("8") }) // 洋蔥模型(如果有await, 則需要在每個中間件函數前面加async next前面加上 await, ha)
app.use(async(ctx, next) =>{ console.log("9")
const res = await axios.get("http://7yue.pro") next()
// 對資源的操作 都是異步的 讀文件 發送http請求 操作數據庫
console.log('10') return "seven" })
// 打印結果為7 9 10 8
// 中間件(函數) return 會被強制轉換成一個 Promise app.listen(3000);
轉載于:https://www.cnblogs.com/fengch/p/11424949.html
總結
以上是生活随笔為你收集整理的node中间件KOA函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RuntimeError: Workin
- 下一篇: Appium+Python移动端 实战—