在知乎上被大佬們夸的不要不要的nodejs 框架避消,號(hào)稱宇宙最快水醋,
本菜鳥就是想實(shí)現(xiàn)一個(gè)數(shù)組列表的schema 沒(méi)想到要累成這樣才能達(dá)到效果渐白,這樣真的就比express直接擼效率會(huì)高很多嗎借杰?
整個(gè)中文社區(qū)似乎沒(méi)有真正開(kāi)始折騰的碟婆,基本沒(méi)搜到一些任何成型的項(xiàng)目和文檔电抚,因此把這個(gè)段垃圾代碼貼上來(lái)給大家吐吐槽。
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}`)
})