fastify 基于json-schema的框架数组问题

在知乎上被大佬们夸的不要不要的nodejs 框架,号称宇宙最快,
本菜鸟就是想实现一个数组列表的schema 没想到要累成这样才能达到效果,这样真的就比express直接撸效率会高很多吗?

整个中文社区似乎没有真正开始折腾的,基本没搜到一些任何成型的项目和文档,因此把这个段垃圾代码贴上来给大家吐吐槽。

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}`)
})
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容