Node-RED配置

配置文件

可通过配置文件配置Node-RED

配置文件在哪?

Node-RED启动时,会在Node-RED用户目录下寻找setting.js文件,~/.node-redWindows中为系统盘:\Users\当前用户\.node-red)。若找不到,将拷贝一份默认的配置文件到目录并使用它
或者,可使用命令行参数--settingNode-RED启动时指向不同的文件
如果想在Node-RED未启动过时编辑配置文件,可从这里手动拷贝默认配置文件
如果不确定Node-RED使用的是哪个配置文件,可以在Node-RED启动时检阅日志输出。它将记录文件的完整路径

22 Jun 12:34:56 - [info] Settings file  : /Users/nol/.node-red/settings.js

编辑配置文件

配置文件运行时加载为Node.js模块,导出包含键/值对的JavaScript对象
默认配置文件带有很多被注释的选项。例如,格式化流程的选项使得更易于阅读:

//flowFilePretty: true,

移除行首//启用选项
如果添加新的选项到文件,确保添加逗号来分隔前一个或后一个选项
如果文件中又语法错误,Node-RED将无法启动。日志输出将表明错误出处

配置选项

作为普通应用运行时,从配置文件加载配置
作为嵌入式应用运行时,通过调用RED.init()传递配置选项。然而,以这种模式运行时,某些属性将被忽略并留给嵌入式应用实现

运行时配置

  • flowFile
    存储流程的文件。默认:flows_.json

  • userDir
    存储所有用户数据的目录,诸如流程、证书文件和所有的库数据。默认:$HOME/.node-redWindows下为系统盘:\Users\当前用户.node-red

  • nodesDir
    查找额外安装的节点的目录。Node-REDuserDir目录下查找节点。这个属性允许查找额外的目录,因此节点可被安装在Node-RED安装结构之外。默认:$HOME/.node-red/nodesWindows下为系统盘:\Users\当前用户.node-red\node_modules

  • uiHost
    监听连接的接口。默认:0.0.0.0 - 所有IPV4接口
    仅单个

  • uiPort
    编辑器界面的端口。默认:1880
    仅单个

  • httpAdminRoot
    编辑器界面的根路径。如果设置为false,禁用所有的管理端点。这包括API端点和编辑器界面。关于仅禁用编辑器界面,查看后面的disableEditor属性
    默认:/

  • httpAdminMiddleware
    添加到所有管理路由的一个或一组HTTP中间方法(过滤器)。具体格式文档看这里

    httpAdminMiddleware: function(req,res,next) {
      // Perform any processing on the request.
      // Be sure to call next() if the request should be passed on
    }
    
  • httpNodeRoot
    提供HTTP端点的节点的根路径(如request in)。如果设置为false,禁用所有基于节点的HTTP端点。默认:/

  • httpNodeAuth
    启用节点HTTP基础认证。

  • httpRoot
    同时设置管理端点和节点端点的根路径。它将覆盖httpAdminRoot httpNodeRoot的值

  • https
    使用这里定义的指定选项对象启用https
    仅单个

  • disableEditor
    如果设置为true,运行时不再提供编辑器界面。管理api端点仍然有效。默认:false

  • httpStatic
    提供静态网页内容的本地目录。

  • httpStaticAuth
    启用静态内容HTTP基础认证

  • httpNodeCors
    根据定义为提供HTTP端点的节点启用跨域资源共享

  • httpNodeMiddleware
    添加到所有http in节点的一个或一组HTTP中间方法。允许又需要的节点自定义处理,诸如权限认证。具体格式文档看这里

    httpNodeMiddleware: function(req,res,next) {
        // Perform any processing on the request.
        // Be sure to call next() if the request should be passed
        // to the relevant HTTP In node.
    }
    
  • logging
    目前仅支持控制台日志。可指定各种日志级别。选项如下:

    • fatal - 仅记录造成应用无法使用的错误
    • error - 记录具体请求的致命错误 + fatal
    • warn - 记录非致命问题 + error + fatal
    • info - 记录应用的常规运行信息 + warn + error + fatal
    • debug - 记录较info更为冗余的信息 + info + warn + error + fatal
    • tarce - 记录非常详细的日志 + debug + info + warn + error + fatal

    默认为info级别。对于嵌入式设备有限的闪存,你也许希望设置为fatal来最小化对“磁盘”的写入

  • externalModules
    配置运行时如何处理外部npm模块。包括:

    • 编辑器是否允许安装新的节点模块
    • 节点,诸如function节点是否被允许有自己的动态配置的依赖

    allowList/denyList选项用于运行时限制安装/加载的模块。可使用*作为通配符匹配任何

    externalModules: {
       autoInstall: false,
       autoInstallRetry: 30,
       palette: {
          allowInstall: true,
          allowUpload: true,
          allowList: [],
          denyList: []
       },
       modules: {
          allowInstall: true,
          allowList: [],
          denyList: []
       }
    }
    

编辑器配置

  • adminAuth
    启用编辑器和管理API的用户等级保护
  • paletteCategories
    定义组件栏中类别排序。不在列表中的节点类别将添加到组件栏的底部。如果未设置,使用如下默认排序:
    ['subflows', 'common', 'function', 'network', 'sequence', 'parser', 'storage'],
    
    *注意:*子流程类别为空且在组件栏中不可见,直到用户创建一个新的子流程

编辑器主题

使用如下配置对象可改变编辑器的主题。所有部分均为可选。

editorTheme: {
    page: {
        title: "Node-RED",
        favicon: "/absolute/path/to/theme/icon",
        css: "/absolute/path/to/custom/css/file",
        scripts: [ "/absolute/path/to/custom/script/file", "/another/script/file"]
    },
    header: {
        title: "Node-RED",
        image: "/absolute/path/to/header/image", // or null to remove image
        url: "http://nodered.org" // optional url to make the header text/image a link to this url
    },
    deployButton: {
        type:"simple",
        label:"Save",
        icon: "/absolute/path/to/deploy/button/image" // or null to remove image
    },
    menu: { // Hide unwanted menu items by id. see packages/node_modules/@node-red/editor-client/src/js/red.js:loadEditor for complete list
        "menu-item-import-library": false,
        "menu-item-export-library": false,
        "menu-item-keyboard-shortcuts": false,
        "menu-item-help": {
            label: "Alternative Help Link Text",
            url: "http://example.com"
        }
    },
    tours: false, // disable the Welcome Tour for new users
    userMenu: false, // Hide the user-menu even if adminAuth is enabled
    login: {
        image: "/absolute/path/to/login/page/big/image" // a 256x256 image
    },
    logout: {
        redirect: "http://example.com"
    },
    palette: {
        editable: true, // *Deprecated* - use externalModules.palette.allowInstall instead
        catalogues: [   // Alternative palette manager catalogues
            'https://catalogue.nodered.org/catalogue.json'
        ],
        theme: [ // Override node colours - rules test against category/type by RegExp.
            { category: ".*", type: ".*", color: "#f0f" }
        ]
    },
    projects: {
        enabled: false // Enable the projects feature
    },
    theme: "", // Select a color theme for the editor. See https://github.com/node-red-contrib-themes/theme-collection for a collection of themes to choose from
    codeEditor: {
        lib: "ace", // Select the text editor component used by the editor. Defaults to "ace", but can be set to "ace" or "monaco"
        options: {
            // The following only apply if the editor is set to "monaco"
            theme: "vs", // Select a color theme for the text editor component. Must match the file name of a theme in packages/node_modules/@node-red/editor-client/src/vendor/monaco/dist/theme
        }
    }
},

仪表盘

  • ui
    指定附加在节点上的Node-RED-Dashboard的主路径(相对于任何已定义的httpNodeRoot
    ui : { path: “mydashboard” },
    

节点配置

任何节点类型都可在文件中定义自身提供的设置

  • functionGlobalContext
    方法节点 - 附加到全局方法上下文中的对象集合。例如,
    functionGlobalContext: { osModule:require('os') }
    
    可在function节点中访问:
    var myos = global.get('osModule');
    

注意:Node-REDv0.13之前,全局上下文作为context的子属性访问:

context.global.foo = "bar";
var osModule = context.global.osModule;

以上方法仍然支持,但出于对global.get/global.set方法的推荐不赞成使用

  • functionExternalModules
    值为true时,function节点的Setup选项卡允许添加方法可用的额外模块。默认:false
  • debugMaxLength
    发送至调试栏的每条消息的最大长度,单位为字符。默认:1000
  • mqttReconnectTime
    MQTT连接丢失时,重新连接前等待的毫秒数。默认:5000
  • serialReconnectTime
    重新打开串口前等待的毫秒数。默认:5000
  • socketReconnectTime
    重新连接TCP前等待的毫秒数。默认:10000
  • socketTimeout
    TCP连接超时时间的毫秒数。默认:120000

安全配置

用户名/密码基础认证

取消配置文件中adminAuth的属性注释
可定义多个拥有不同权限的用户

adminAuth: {
    type: "credentials",
    users: [
        {
            username: "admin",
            password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.",
            permissions: "*"
        },
        {
            username: "george",
            password: "$2b$08$wuAqPiKJlVN27eF5qJp.RuQYuy6ZYONW7a/UWYxDTtwKFCdB8F19y",
            permissions: "read"
        }
    ]
}

上述例子定义了两个用户。admin拥有编辑器的所有权限,george只有查看权限。
注意密码使用的是bcrypt算法

生成密码散列值

如果使用Node-RED 1.1.0或之后的版本,可使用以下命令:

node-red admin hash-pw

根据提示输入密码后,将获取的值粘贴至配置文件中

设置默认用户

某些情况下,希望给与所有用户一定的访问级别。为adminAuth设置添加default属性来定义默认只读

adminAuth: {
    type: "credentials",
    users: [ /* list of users */ ],
    default: {
        permissions: "read"
    }
}

更多用法阅读官网及配置文件

你可能感兴趣的:(Node-RED学习,学习,javascript,node.js)