将 fastify 实例传递给顶层的控制器
Pass fastify instance to controllers at a top level
不是将 fastify instance
传递给每个函数,而是如何在顶层只传递一次它以便控制器中的所有函数都可以使用它?
// route file
import { FastifyPluginAsync } from 'fastify'
import RootController from '../root.controller'
const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.get('/', RootController.index(fastify))
fastify.get('/show', RootController.show(fastify))
}
export default root
// controller file
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
export default {
index: (fastify: FastifyInstance) => async (request: FastifyRequest, reply: FastifyReply) => {
const hugs = fastify.someSupport()
reply.send({ status: 'ok', love: hugs })
},
show: (fastify: FastifyInstance) => async (request: FastifyRequest, reply: FastifyReply) => {
const hugs = 'extra ' + fastify.someSupport()
reply.send({ status: 'ok', love: hugs })
}
}
答案是使用闭包进行简单、机械的重构:
// controller file
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
export default async function installRootController(fastify: FastifyInstance, opts): Promise<void> {
fastify.get('/', async (request: FastifyRequest, reply: FastifyReply) => {
const hugs = fastify.someSupport()
reply.send({ status: 'ok', love: hugs })
});
fastify.get('/show', async (request: FastifyRequest, reply: FastifyReply) => {
const hugs = 'extra ' + fastify.someSupport()
reply.send({ status: 'ok', love: hugs })
});
};
内部函数(/
和 /show
的异步处理程序)可以访问 fastify
,因为它可以从周围函数的词法范围访问。这使您能够将整个“控制器”封装为 Fastify 插件,准备好像这样安装:
fastify.register(installRootController);
请注意,“routes”文件现在没有用了。
不是将 fastify instance
传递给每个函数,而是如何在顶层只传递一次它以便控制器中的所有函数都可以使用它?
// route file
import { FastifyPluginAsync } from 'fastify'
import RootController from '../root.controller'
const root: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
fastify.get('/', RootController.index(fastify))
fastify.get('/show', RootController.show(fastify))
}
export default root
// controller file
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
export default {
index: (fastify: FastifyInstance) => async (request: FastifyRequest, reply: FastifyReply) => {
const hugs = fastify.someSupport()
reply.send({ status: 'ok', love: hugs })
},
show: (fastify: FastifyInstance) => async (request: FastifyRequest, reply: FastifyReply) => {
const hugs = 'extra ' + fastify.someSupport()
reply.send({ status: 'ok', love: hugs })
}
}
答案是使用闭包进行简单、机械的重构:
// controller file
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'
export default async function installRootController(fastify: FastifyInstance, opts): Promise<void> {
fastify.get('/', async (request: FastifyRequest, reply: FastifyReply) => {
const hugs = fastify.someSupport()
reply.send({ status: 'ok', love: hugs })
});
fastify.get('/show', async (request: FastifyRequest, reply: FastifyReply) => {
const hugs = 'extra ' + fastify.someSupport()
reply.send({ status: 'ok', love: hugs })
});
};
内部函数(/
和 /show
的异步处理程序)可以访问 fastify
,因为它可以从周围函数的词法范围访问。这使您能够将整个“控制器”封装为 Fastify 插件,准备好像这样安装:
fastify.register(installRootController);
请注意,“routes”文件现在没有用了。