fastify 基于json-schema的框架數(shù)組問題

在知乎上被大佬們夸的不要不要的nodejs 框架,號稱宇宙最快,
本菜鳥就是想實(shí)現(xiàn)一個(gè)數(shù)組列表的schema 沒想到要累成這樣才能達(dá)到效果,這樣真的就比express直接擼效率會高很多嗎?

整個(gè)中文社區(qū)似乎沒有真正開始折騰的,基本沒搜到一些任何成型的項(xiàng)目和文檔,因此把這個(gè)段垃圾代碼貼上來給大家吐吐槽。

const fastify = require('fastify')()
const cnblog = require('./sites/cnblog')
const listsc =[]

for(let i=0;i<20;i++){
    listsc.push(
        {
          "type": "object",
          properties: {
              id: {
                type: 'number'
              },
              title: {
                type: 'string'
              },
              link: {
                type: 'string'
              },
              desc: {
                type: 'string'
              }
          }
        }
    )
}
fastify.route({
  method: 'GET',
  url: '/',
  schema: {
    // request needs to have a querystring with a `name` parameter
    querystring: {
      name: { type: 'string' }
    },
    // the response needs to be an object with an `hello` property of type 'string'
    response: {
      200:{
        "type": "array",
        "items": listsc,
        maxItems: 20
      }
    }
  },
  // this function is executed for every request before the handler is executed
  beforeHandler:  (request, reply, done) => {
    // E.g. check authentication
    done()
  },
  handler: (request, reply) => {
      cnblog('axios').then((data)=>{
         const li = []
         for(i in data) {
             li.push(data[i])
         }
         reply.send(li)
      })
  }
})

fastify.listen(3000, (err) => {
  if (err) {
   console.log(err)
    process.exit(1)
  }
  fastify.log.info(`server listening on ${fastify.server.address().port}`)
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容