fastify-autoload 一个方便的插件动态加载包
fastify-autoload 是一个方便的fastify 插件加载工具,我们可以基于路径直接加载开发的插件
参考使用
- 配置
const Fastify = require('fastify')
const path = require("path")
const autoLoad = require('@fastify/autoload');
const app = Fastify({
logger: true,
ignoreDuplicateSlashes: true,
}
);
app.register(autoLoad, {
dir: path.join(__dirname, 'plugins')
})
app.listen({
port: 3000,
host: '0.0.0.0',
backlog: 511
}, (err, address) => {
if (err) {
console.log(err)
process.exit(1)
} else {
console.log(`Server listening on ${address}`)
}
})
- 插件开发
可以是标注的插件,也可以是基于fastify 提供的 fastify-plugin包开发的(这个也是不少官方以及社区插件推荐的开发模式)
基于fastify-plugin开发的插件
const fp = require('fastify-plugin')
function appsPlugin(fastify, opts, next) {
fastify.get("/app", async (req, res) => {
let info = {
id: "dalong app demo"
}
console.log(`------${JSON.stringify(info)}----`)
res.send(info)
})
next()
}
// 通过astify-plugin包装插件
module.exports = fp(appsPlugin, {
name:"app"
})
说明
对于设计开发比较推荐的是可以结合@vercel/ncc, 这样我们打开的应用就比较方便了,在我们的core 部分只提供通用包,每个插件自己开发,然后就是一些标注的commonjs 模块代码(直接基于文件目录加载存储)
打包之后的效果
参考资料
https://www.npmjs.com/package/@fastify/autoload
https://github.com/fastify/fastify-autoload
https://github.com/vercel/ncc#readme
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2022-10-17 dremio HomeFileSystemStoragePlugin简单介绍
2022-10-17 dremio 的服务管理
2022-10-17 dremio 的几种插件
2022-10-17 dremio AbstractRecordReader 简单说明一
2022-10-17 dremio Operator 简单说明
2021-10-17 nomad-pack nomad的仓库特性
2021-10-17 zalando 最新关于微前端的一些实践文章