Node-Red自定义流程模板(Flow)

Node-Red支持导入导出Flow(s),可以通过图形编辑界面导入现有Flow(s)或者导出现有Flow(s)。本文将着重介绍通过API的方式创建Flow(s),或获取/导出Flow(s)

API

首先我们将先介绍API的鉴权过程:

前提

想要通过鉴权调用adminAuthapi之前,我们需要确定一下,是否已经正确设置了sttings.jsadminAuth对象(集成模式亦是如此)。如果我们没有设置这个对象,那么任何人都可以在未授权的情况下调用adminAuthapi,也就失去了鉴权的意义。

step 1 获取Token

调用/auth/tokenHTTP POST请求 用于交换用户凭据以获取访问令牌。body必须提供以下参数:

  • client_id - 标识客户端。
    目前,必须是 node-red-adminnode-red-editor。建议使用node-red-editor,因为默认登录用户使用的就是node-red-editor,否则将会出现Flow数据对应不上的问题
  • grant_type - 必须是passport
  • scope - 一个以空格分隔的请求权限列表。
    目前,必须是*read
  • username - 要进行身份验证的用户名
  • password - 验证的密码

Request Header需要携带以下参数:

  • Authorization- 获取token接口填写Bearer
  • Content-Type- application/json

Response Body返回值:

  • access_token- 令牌
  • expires_in- 超时时间
  • token_type- 令牌类型

请求事例,这里以postman为例:


requestHeader

requestBody

responseBody

step 2 调用API

添加单个Flow,调用/flow 的 HTTP POST请求
注意,配置对象必须至少包含节点属性。添加时将为Flow分配一个新的id。 如果提供的Flow配置对象包含一个 id 字段,它将被替换,并且所有节点的 z 属性都会更新以匹配。Flow中的所有节点都必须具有唯一的 id 属性。 如果任何 id 已在使用中,则该请求将被拒绝。
Request Header需要携带以下参数:

  • Authorization- 填写Bearer [token]
    这里的 token为授权返回的token,注意Bearer token中间有空格
  • Content-Type- application/json

Request Body参数:

{
        "id": "a5e2e7f892d1d6e3",
        "type": "tab",
        "label": "http_request_response",
        "nodes":[
          {
        "id": "71bab3bcd6387e8e",
        "type": "http in",
        "z": "a5e2e7f892d1d6e3",
        "name": "获取平台分类",
        "url": "/getCategory",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 230,
        "y": 220,
        "wires": [
            [
                "37a7427e637a53e4"
            ]
        ]
    },
    {
        "id": "37a7427e637a53e4",
        "type": "http request",
        "z": "a5e2e7f892d1d6e3",
        "name": "获取平台分类接口",
        "method": "GET",
        "ret": "txt",
        "paytoqs": "query",
        "url": "https://example.com/path/api?a=2&t=2",
        "tls": "",
        "persist": true,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 470,
        "y": 220,
        "wires": [
            [
                "fe9e5605a7e6f8a2"
            ]
        ]
    },
    {
        "id": "fe9e5605a7e6f8a2",
        "type": "function",
        "z": "a5e2e7f892d1d6e3",
        "name": "处理平台信息",
        "func": "const result = msg.payload;\nif(msg.statusCode !=200){\n    msg.payload = JSON.stringify({message:\"node found\"});\n    msg.statusCode =200;\n    return msg;\n}else{\n    msg.payload = JSON.parse(result);\n    node.warn(msg);\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 220,
        "wires": [
            [
                "5ed3f42b59f47353"
            ]
        ]
    },
    {
        "id": "5ed3f42b59f47353",
        "type": "http response",
        "z": "a5e2e7f892d1d6e3",
        "name": "返回平台分类",
        "statusCode": "200",
        "headers": {
            "content-type": "application/json"
        },
        "x": 1040,
        "y": 220,
        "wires": []
    },
    {
        "id": "8b28e71cc0fa4b2e",
        "type": "catch",
        "z": "a5e2e7f892d1d6e3",
        "name": "捕获获取平台信息接口",
        "scope": [
            "37a7427e637a53e4"
        ],
        "uncaught": false,
        "x": 480,
        "y": 120,
        "wires": [
            [
                "25ef2ec0efa5db7a"
            ]
        ]
    },
    {
        "id": "25ef2ec0efa5db7a",
        "type": "debug",
        "z": "a5e2e7f892d1d6e3",
        "name": "打印错误日志",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": false,
        "complete": "error",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 800,
        "y": 120,
        "wires": []
    }
        ]
    },

Response Body

{
    "id": "67db1f444409c4dd"
}

这样编辑页面就会出现以下Flow以及子节点:

编辑页面

上述方法为API的新增,下面将介绍API方式获取Flow(s)
获取单个Flow,调用/flow/:id 的 HTTP GET请求
Request Header需要携带以下参数:

  • Authorization- 填写Bearer [token]
    这里的 token为授权返回的token,注意Bearer token中间有空格
  • Content-Type- application/json
    同样以postman为例
    requestHeader

返回值:

{
    "id": "a5e2e7f892d1d6e3",
    "label": "http_request_response",
    "disabled": false,
    "info": "",
    "env": [],
    "nodes": [
        {
            "id": "71bab3bcd6387e8e",
            "type": "http in",
            "z": "a5e2e7f892d1d6e3",
            "name": "获取平台分类",
            "url": "/getCategory",
            "method": "get",
            "upload": false,
            "swaggerDoc": "",
            "x": 230,
            "y": 220,
            "wires": [
                [
                    "37a7427e637a53e4"
                ]
            ]
        },
        {
            "id": "37a7427e637a53e4",
            "type": "http request",
            "z": "a5e2e7f892d1d6e3",
            "name": "获取平台分类接口",
            "method": "GET",
            "ret": "txt",
            "paytoqs": "query",
            "url": "https://www.ttcdw.cn/m/open/teacher/v1/category?platformId=13145854983311&type=1",
            "tls": "",
            "persist": true,
            "proxy": "",
            "authType": "",
            "senderr": false,
            "x": 470,
            "y": 220,
            "wires": [
                [
                    "fe9e5605a7e6f8a2"
                ]
            ]
        },
        {
            "id": "fe9e5605a7e6f8a2",
            "type": "function",
            "z": "a5e2e7f892d1d6e3",
            "name": "处理平台信息",
            "func": "const result = msg.payload;\nif(msg.statusCode !=200){\n    msg.payload = JSON.stringify({message:\"node found\"});\n    msg.statusCode =200;\n    return msg;\n}else{\n    msg.payload = JSON.parse(result);\n    node.warn(msg);\n    return msg;\n}\n",
            "outputs": 1,
            "noerr": 0,
            "initialize": "",
            "finalize": "",
            "libs": [],
            "x": 680,
            "y": 220,
            "wires": [
                [
                    "5ed3f42b59f47353"
                ]
            ]
        },
        {
            "id": "5ed3f42b59f47353",
            "type": "http response",
            "z": "a5e2e7f892d1d6e3",
            "name": "返回平台分类",
            "statusCode": "200",
            "headers": {
                "content-type": "application/json"
            },
            "x": 1040,
            "y": 220,
            "wires": []
        },
        {
            "id": "8b28e71cc0fa4b2e",
            "type": "catch",
            "z": "a5e2e7f892d1d6e3",
            "name": "捕获获取平台信息接口",
            "scope": [
                "37a7427e637a53e4"
            ],
            "uncaught": false,
            "x": 480,
            "y": 120,
            "wires": [
                [
                    "25ef2ec0efa5db7a"
                ]
            ]
        },
        {
            "id": "25ef2ec0efa5db7a",
            "type": "debug",
            "z": "a5e2e7f892d1d6e3",
            "name": "打印错误日志",
            "active": true,
            "tosidebar": true,
            "console": true,
            "tostatus": false,
            "complete": "error",
            "targetType": "msg",
            "statusVal": "",
            "statusType": "auto",
            "x": 800,
            "y": 120,
            "wires": []
        }
    ]
}

Runtime Function

Node-Red另外提供runtime function方式进行数据导入,以express引入Node-Red为例,方法如下:

const nodeRed = require("node-red);
const flows = [
{
    "label": "label1",
    "info": "",
    "nodes": [],
},
{
    "label": "label2",
    "info": "",
    "nodes": [],
}
];
nodeRed.runtime.storage.saveFlows(flows);//注意这里返回的是Promise对象!

这里为Node-Red的内部方法,所以不需要走授权。执行结果这里不具体举例了,自己尝试一下。获取方法也是一样:

const nodeRed = require("node-red);
nodeRed.runtime.storage.getFlows();//注意这里返回的是Promise对象!

以上就是Node-Red自定义模板流程的全部内容了,感谢阅读,下一篇是Node-Red的自定义节点(Node)。

你可能感兴趣的:(Node-Red自定义流程模板(Flow))