Vue.jsvuetifyVue.js开发技巧

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

2018-05-24  本文已影响2人  北方蜘蛛

在知乎上被大佬们夸的不要不要的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}`)
})
上一篇 下一篇

猜你喜欢

热点阅读