跳转至内容
  • 版块
  • 最新
  • 热门
  • 标签
  • 积分榜
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(不使用皮肤)
  • 不使用皮肤
折叠
品牌标识

百得社区

  1. 主页
  2. 技术分享
  3. nodebb开发插件的一点个人心得

nodebb开发插件的一点个人心得

已定时 已固定 已锁定 已移动 技术分享
1 帖子 1 发布者 105 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • 重复过往重 离线
    重复过往重 离线
    重复过往
    写于 最后由 重复过往 编辑
    #1
    1. 首先从 nodebb-plugin-quickstart 这里拉一个模板出来

    2. 核心就是plugin.json

      {
        "id": "nodebb-plugin-quickstart",  // 插件id
        "url": "https://github.com/NodeBB/nodebb-plugin-quickstart",
        "library": "./library.js", // 在服务端执行的逻辑 比如这次需求用到的拦截登录、注册路由 等各种钩子在这里操作
        "hooks": [ // 这个就是你用得钩子和钩子里对应的方法名称 在./library.js中注册
          { "hook": "action:auth.overrideLogin", "method": "customLogin" }, // 拦截login
          { "hook": "static:app.load", "method": "init" }
        ],
        "staticDirs": { // 应该是静态资源 这次需求没用到
          "static": "./static"
        },
        "scss": ["scss/quickstart.scss"], // 应该是scss覆盖编写的 这次需求没用到
        "scripts": ["public/lib/main.js"], // 这个就是在客户端全局执行的js文件 里面也能用钩子 演示demo中有
        "acpScripts": ["public/lib/acp-main.js"], // 这个是在后台管理全局中的js文件 
        "modules": { // 这个就是在客户端特定路由中用得js文件 具体绑定逻辑可以看demo中的注释
          "../client/quickstart.js": "./public/lib/quickstart.js",  // 域名/quickstart
      		"../admin/plugins/quickstart.js": "./public/lib/admin.js" // 域名/后管/quickstart 
        },
        "templates": "templates" // 模板列表 你如果需要开发自定义页面就需要用到这个
      }
      
      
    3. 本地开发时,或者私有化部署插件时可以使用npm link

    4. 如果你想做客户端不登录就强制跳转到登录页,需要在客户端全局 script 中加逻辑而不是在 library.js 中,library.js 中的钩子拿不到客户端的请求 url 只能拿到静态资源
      在public/main.js中

    "use strict";
    
    /**
     * This file shows how client-side javascript can be included via a plugin.
     * If you check `plugin.json`, you'll see that this file is listed under "scripts".
     * That array tells NodeBB which files to bundle into the minified javascript
     * that is served to the end user.
     *
     * There are two (standard) ways to wait for when NodeBB is ready.
     * This one below executes when NodeBB reports it is ready...
     */
    
    (async () => {
      const hooks = await app.require("hooks");
    
      hooks.on("action:app.load", () => {
        console.log("NodeBB is ready!", app.user);
    
        // called once when nbb has loaded
      });
    
      hooks.on("action:ajaxify.end", (/* data */) => {
        // called everytime user navigates between pages including first load
       
      });
    })();
    
    /**
     * ... and this one reports when the DOM is loaded (but NodeBB might not be fully ready yet).
     * For most cases, you'll want the one above.
     */
    
    $(document).ready(function () {
      // ...
    });
    
    1. 开发中,如果更新的是library服务端相关的插件代码,要重启下nodebb查看效果,如果更新的是client客户端相关的代码,要重新构建下静态资源 nodebb build,,再启动nodebb

    2. 我用的nodebb的镜像ghcr.io/nodebb, 通过 docker exec -u 0 -it 容器名称或id /bin/bash 进入镜像容器,这个镜像中没有git,所以我将我开发的插件压缩成.gz的包,通过docker cp nodebb-plugin.tar.gz 容器名称或id:/usr/src/app拷贝到了容器中 在容器中进行解压,在通过npm link 集成到了nodebb服务中,集成后在nodebb后台管理的插件列表中能看到已安装该插件

    3. 我回归了原始部署 docker部署太慢了而且在容器中集成本地插件时容易出权限问题 还是直接拉代码服务器构建运行方便 - -

    4. config.json 中 "upload_path": "目录" 是指定上传的文件目录

    5. 我写了个简单的过滤敏感词的插件 ,里面有简单的后台管理录入数据读数据的逻辑 可以做个参考,最好的学习资料其实还是nodebb-plugin-quickstart

    这次不得不冲了!

    1 条回复 最后回复
    1

    Powered by NodeBB | Contributors
    • 登录

    • 登录或注册以进行搜索。
    • 第一个帖子
      最后一个帖子
    0
    • 版块
    • 最新
    • 热门
    • 标签
    • 积分榜
    • 用户
    • 群组