Node-Red支持導入導出Flow(s),可以通過圖形編輯界面導入現(xiàn)有Flow(s)或者導出現(xiàn)有Flow(s)。本文將著重介紹通過API的方式創(chuàng)建Flow(s),或獲取/導出Flow(s)。
API
首先我們將先介紹API的鑒權(quán)過程:
前提
想要通過鑒權(quán)調(diào)用adminAuth的api之前,我們需要確定一下,是否已經(jīng)正確設置了sttings.js中adminAuth對象(集成模式亦是如此)。如果我們沒有設置這個對象,那么任何人都可以在未授權(quán)的情況下調(diào)用adminAuth的api,也就失去了鑒權(quán)的意義。
step 1 獲取Token
調(diào)用/auth/token 的 HTTP POST請求 用于交換用戶憑據(jù)以獲取訪問令牌。body必須提供以下參數(shù):
-
client_id- 標識客戶端。
目前,必須是node-red-admin或node-red-editor。建議使用node-red-editor,因為默認登錄用戶使用的就是node-red-editor,否則將會出現(xiàn)Flow數(shù)據(jù)對應不上的問題 -
grant_type- 必須是passport -
scope- 一個以空格分隔的請求權(quán)限列表。
目前,必須是*或read。 -
username- 要進行身份驗證的用戶名 -
password- 驗證的密碼
Request Header需要攜帶以下參數(shù):
-
Authorization- 獲取token接口填寫Bearer -
Content-Type-application/json
Response Body返回值:
-
access_token- 令牌 -
expires_in- 超時時間 -
token_type- 令牌類型
請求事例,這里以postman為例:



step 2 調(diào)用API
添加單個Flow,調(diào)用/flow 的 HTTP POST請求
注意,配置對象必須至少包含節(jié)點屬性。添加時將為Flow分配一個新的id。 如果提供的Flow配置對象包含一個 id 字段,它將被替換,并且所有節(jié)點的 z 屬性都會更新以匹配。Flow中的所有節(jié)點都必須具有唯一的 id 屬性。 如果任何 id 已在使用中,則該請求將被拒絕。
Request Header需要攜帶以下參數(shù):
-
Authorization- 填寫Bearer [token]
這里的token為授權(quán)返回的token,注意Bearer token中間有空格 -
Content-Type-application/json
Request Body參數(shù):
{
"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"
}
這樣編輯頁面就會出現(xiàn)以下Flow以及子節(jié)點:

上述方法為API的新增,下面將介紹API方式獲取Flow(s)
獲取單個Flow,調(diào)用/flow/:id 的 HTTP GET請求
Request Header需要攜帶以下參數(shù):
-
Authorization- 填寫Bearer [token]
這里的token為授權(quán)返回的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方式進行數(shù)據(jù)導入,以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的內(nèi)部方法,所以不需要走授權(quán)。執(zhí)行結(jié)果這里不具體舉例了,自己嘗試一下。獲取方法也是一樣:
const nodeRed = require("node-red);
nodeRed.runtime.storage.getFlows();//注意這里返回的是Promise對象!
以上就是Node-Red自定義模板流程的全部內(nèi)容了,感謝閱讀,下一篇是Node-Red的自定義節(jié)點(Node)。
