最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談KOA2 Restful方式路由初探

 更新時間:2019年03月14日 15:07:08   作者:able  
這篇文章主要介紹了淺談KOA2 Restful方式路由初探,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言

最近考慮將服務器資源整合一下,作為多端調用的API

看到Restful標準和ORM眼前一亮,但是找了不少版本路由寫的都比較麻煩,于是自己折騰了半天

API庫結構

考慮到全部對象置于頂層將會造成對象名越來長,同時不便于維護,故采取部分的分層結構

如workflow模塊內的prototypes,instances等等,分層的深度定義為層級

可訪問的對象集合(collection)的屬性滿足Restful設計

 -- workflow(category)
  -- prototypes(collection)
    -- [method] ...
    -- [method] ... 
  -- instances(collection)
 -- users(collection)
   --[method] List     #get :object/
   --[method] Instance   #get :object/:id
 -- ...
 -- ...

RESTFUL API 接口

將Restful API接口進行標準化命名

.get('/', ctx=>{ctx.error('路徑匹配失敗')})        
.get('/:object', RestfulAPIMethods.List)
.get('/:object/:id', RestfulAPIMethods.Get)
.post('/:object', RestfulAPIMethods.Post)
.put('/:object/:id', RestfulAPIMethods.Replace)
.patch('/:object/:id', RestfulAPIMethods.Patch)
.delete('/:object/:id', RestfulAPIMethods.Delete)
.get('/:object/:id/:related', RestfulAPIMethods.Related)
.post('/:object/:id/:related', RestfulAPIMethods.AddRelated)
.delete('/:object/:id/:related/:relatedId', RestfulAPIMethods.DelRelated)

API對象

這個文件是來自微信小程序demo,覺得很方便就拿來用了,放于需要引用的根目錄,引用后直接獲得文件目錄結構API對象

const _ = require('lodash')
const fs = require('fs')
const path = require('path')

/**
 * 映射 d 文件夾下的文件為模塊
 */
const mapDir = d => {
  const tree = {}

  // 獲得當前文件夾下的所有的文件夾和文件
  const [dirs, files] = _(fs.readdirSync(d)).partition(p => fs.statSync(path.join(d, p)).isDirectory())

  // 映射文件夾
  dirs.forEach(dir => {
    tree[dir] = mapDir(path.join(d, dir))
  })

  // 映射文件
  files.forEach(file => {
    if (path.extname(file) === '.js') {
      tree[path.basename(file, '.js')] = require(path.join(d, file))
      tree[path.basename(file, '.js')].isCollection = true
    }
  })

  return tree
}



// 默認導出當前文件夾下的映射
module.exports = mapDir(path.join(__dirname))

koa-router分層路由的實現(xiàn)

創(chuàng)建多層路由及其傳遞關系

執(zhí)行順序為

 1 -- 路徑匹配
    -- 匹配到‘/'結束
    -- 匹配到對應的RestfulAPI執(zhí)行并結束
    -- 繼續(xù)
 2 -- 傳遞中間件 Nest
 3 -- 下一級路由
 4 -- 循環(huán) to 1

const DefinedRouterDepth = 2
let routers = []
for (let i = 0; i < DefinedRouterDepth; i++) {
  let route = require('koa-router')()
  if (i == DefinedRouterDepth - 1) {
    // 嵌套路由中間件
    route.use(async (ctx, next) => {
      // 根據版本號選擇庫
      let apiVersion = ctx.headers['api-version']
      ctx.debug(`------- (API版本 [${apiVersion}]) --=-------`)
       if (!apiVersion) {
        ctx.error('版本號未標記')
        return
      }
      let APIRoot = null
      try {
        APIRoot = require(`../restful/${apiVersion}`)
      } catch (e) {
        ctx.error ('API不存在,請檢查Header中的版本號')
        return
      }
      ctx.debug(APIRoot)
      ctx.apiRoot = APIRoot
      ctx.debug('---------------------------------------------')
      // for(let i=0;i<)
      await next()
    })
  }
  route
    .get('/', ctx=>{ctx.error('路徑匹配失敗')})
    .get('/:object', RestfulAPIMethods.List)
    .get('/:object/:id', RestfulAPIMethods.Get)
    .post('/:object', RestfulAPIMethods.Post)
    .put('/:object/:id', RestfulAPIMethods.Replace)
    .patch('/:object/:id', RestfulAPIMethods.Patch)
    .delete('/:object/:id', RestfulAPIMethods.Delete)
    .get('/:object/:id/:related', RestfulAPIMethods.Related)
    .post('/:object/:id/:related', RestfulAPIMethods.AddRelated)
    .delete('/:object/:id/:related/:relatedId', RestfulAPIMethods.DelRelated)


  if (i != 0) {
    route.use('/:object', Nest, routers[i - 1].routes())
  }
  routers.push(route)
}
let = router = routers[routers.length - 1]

Nest中間件

將ctx.apiObject設置為當前層的API對象

const Nest= async (ctx, next) => {
  let object = ctx.params.object
  let apiObject = ctx.apiObject || ctx.apiRoot
  if(!apiObject){
    ctx.error('API裝載異常')
    return
  }

  if (apiObject[object]) {
    ctx.debug(`ctx.apiObject=>ctx.apiObject[object]`)
    ctx.debug(apiObject[object])
    ctx.debug(`------------------------------------`)
    ctx.apiObject = apiObject[object]
  } else {
    ctx.error(`API接口${object}不存在`)
    return
  }


  await next()
}

RestfulAPIMethods

let RestfulAPIMethods = {}
let Methods = ['List', 'Get', 'Post', 'Replace', 'Patch', 'Delete', 'Related', 'AddRelated', 'DelRelated']
for (let i = 0; i < Methods.length; i++) {
  let v = Methods[i]
  RestfulAPIMethods[v] = async function (ctx, next) {
    
    let apiObject = ctx.apiObject || ctx.apiRoot
    if (!apiObject) {
      ctx.error ('API裝載異常')
      return
    }
    let object = ctx.params.object
    if (apiObject[object] && apiObject[object].isCollection) {
      ctx.debug(` --- Restful API [${v}] 調用--- `)
      if (typeof apiObject[object][v] == 'function') {
        ctx.state.data = await apiObject[object][v](ctx)
        ctx.debug('路由結束')
        return
        //ctx.debug(ctx.state.data)
      } else {
        ctx.error(`對象${object}不存在操作${v}`)
        return
      }
    }
    ctx.debug(` --- 當前對象${object}并不是可訪問對象 --- `)
    await next()
  }
}

需要注意的點

1、koa-router的調用順序
2、涉及到async注意next()需要加await

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • node.js中的http.createClient方法使用說明

    node.js中的http.createClient方法使用說明

    這篇文章主要介紹了node.js中的http.createClient方法使用說明,本文介紹了http.createClient的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • npm查看鏡像源與切換鏡像源方法詳解

    npm查看鏡像源與切換鏡像源方法詳解

    這篇文章主要為大家介紹了npm查看鏡像源與切換鏡像源方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Node.js學習之內置模塊fs用法示例

    Node.js學習之內置模塊fs用法示例

    這篇文章主要介紹了Node.js學習之內置模塊fs用法,結合實例形式詳細分析了node.js內置模塊fs的基本功能、用法與相關操作注意事項,需要的朋友可以參考下
    2020-01-01
  • node.js中express中間件body-parser的介紹與用法詳解

    node.js中express中間件body-parser的介紹與用法詳解

    這篇文章主要給大家介紹了關于node.js中express中間件body-parser的相關資料,文章通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • 簡單兩步使用node發(fā)送qq郵件的方法

    簡單兩步使用node發(fā)送qq郵件的方法

    這篇文章主要介紹了簡單兩步使用node發(fā)送qq郵件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Nest.js使用multer實現(xiàn)文件上傳功能

    Nest.js使用multer實現(xiàn)文件上傳功能

    這篇文章主要為大家詳細介紹了Nest.js鵝湖使用multer實現(xiàn)文件上傳功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-03-03
  • 基于node.js的fs核心模塊讀寫文件操作(實例講解)

    基于node.js的fs核心模塊讀寫文件操作(實例講解)

    下面小編就為大家?guī)硪黄趎ode.js的fs核心模塊讀寫文件操作(實例講解)。小編覺得挺不錯的,現(xiàn)在就想給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • nvm管理node版本的詳細圖文教程

    nvm管理node版本的詳細圖文教程

    nvm全英文也叫node.js version management,是一個nodejs的版本管理工具,下面這篇文章主要給大家介紹了關于nvm管理node版本的詳細圖文教程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • node.js+postman+mongodb搭建測試注冊接口的實現(xiàn)

    node.js+postman+mongodb搭建測試注冊接口的實現(xiàn)

    本文主要介紹了node.js+postman+mongodb搭建測試注冊接口的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • node.js中 redis 的安裝和基本操作示例

    node.js中 redis 的安裝和基本操作示例

    這篇文章主要介紹了node.js中 redis 的安裝和基本操作,結合實例形式分析了node.js中Redis下載、安裝、數(shù)據類型及基本操作技巧,需要的朋友可以參考下
    2020-02-02

最新評論

无锡市| 商河县| 西乡县| 壤塘县| 永吉县| 久治县| 福清市| 宜川县| 新河县| 武强县| 东城区| 定州市| 临夏市| 商城县| 炉霍县| 永德县| 当雄县| 香格里拉县| 精河县| 屏山县| 正阳县| 湖北省| 涞源县| 张家口市| 浮梁县| 木兰县| 哈密市| 潜山县| 永年县| 城市| 曲麻莱县| 丰都县| 和龙市| 富顺县| 阿拉善盟| 左权县| 和静县| 会宁县| 东平县| 六枝特区| 阿拉善右旗|