介紹
Node-RED背景介紹
• Node-Red是IBM公司開發的一個可視化的編程工具。它容許程序員經過組合各部件來編寫應用程序。這些部件能夠是硬件設備(如:Arduino板子)、Web API(如:WebSocket in和WebSocket out)、功能函數(如:range)或者在線服務(如:email)。
• Node-Red提供基於網頁的編程環境。經過拖拽已定義node到工做區並用線鏈接node建立數據流來實現編程。程序員經過點擊‘Deploy’按鈕實現一鍵保存並執行。程序以JSON字符串的格式保存,方便用戶分享、修改。
• Node-Red基於Node.js,它的執行模型和Node.js同樣,也是事件驅動非阻塞的。理論上,Node.js的全部模塊均可以被封裝成Node-Red的一個或幾個node。(Node.js 是一個基於 Chrome V8 引擎的 JavaScript 運行環境。使用了一個事件驅動、非阻塞式 I/O 的模型,使其輕量又高效。 Node.js 的包管理器 npm是全球最大的開源庫生態系統。)node
安裝
爲了方便,採用docker的方式安裝,附官網python
全部安裝方式:https://nodered.org/docs/getting-started/程序員
docker安裝方式:https://nodered.org/docs/getting-started/dockerdocker
1. 首先拉取鏡像(這裏使用的最新版本latest):
docker pull nodered/node-red-docker
2. 啓動一個容器:
我打算把data文件掛載到主機上,省的每次都去容器內部改配置啥的,查看數據也方便npm
首先在你當前目錄(哪兒均可以)建立一個空文件夾node-red-data,設置權限爲777編程
chmod 777 node-red-data/
當前目錄啓動容器json
docker run -it -p 1880:1880 -v $PWD/node-red-data:/data --name mynodered nodered/node-red-docker
注意:若是不把要掛載的node-red-data文件夾權限改爲777,會報權限錯誤:瀏覽器
> node-red-docker@1.0.0 start /usr/src/node-red > node $NODE_OPTIONS node_modules/node-red/red.js -v $FLOWS "--userDir" "/data" fs.js:1918 binding.copyFile(src, dest, flags); ^ Error: EACCES: permission denied, copyfile '/usr/src/node-red/node_modules/node-red/settings.js' -> '/data/settings.js' at Object.fs.copyFileSync (fs.js:1918:11) at copyFile (/usr/src/node-red/node_modules/fs-extra/lib/copy-sync/copy-sync.js:68:8) at onFile (/usr/src/node-red/node_modules/fs-extra/lib/copy-sync/copy-sync.js:53:25) at getStats (/usr/src/node-red/node_modules/fs-extra/lib/copy-sync/copy-sync.js:48:44) at startCopy (/usr/src/node-red/node_modules/fs-extra/lib/copy-sync/copy-sync.js:38:10) at handleFilterAndCopy (/usr/src/node-red/node_modules/fs-extra/lib/copy-sync/copy-sync.js:33:10) at Object.copySync (/usr/src/node-red/node_modules/fs-extra/lib/copy-sync/copy-sync.js:26:10) at Object.<anonymous> (/usr/src/node-red/node_modules/node-red/red.js:108:20) at Module._compile (module.js:653:30) at Object.Module._extensions..js (module.js:664:10) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-red-docker@1.0.0 start: `node $NODE_OPTIONS node_modules/node-red/red.js -v $FLOWS "--userDir" "/data"` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-red-docker@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /usr/src/node-red/.npm/_logs/2019-09-06T08_12_17_367Z-debug.log
若是docker run 命令帶 -ti 交互參數的話,啓動成功會出現以下文字:bash
# docker run -it -p 1880:1880 -v $PWD/node-red-data:/data --name mynodered nodered/node-red-docker > node-red-docker@1.0.0 start /usr/src/node-red > node $NODE_OPTIONS node_modules/node-red/red.js -v $FLOWS "--userDir" "/data" 6 Sep 08:17:21 - [info] Welcome to Node-RED =================== 6 Sep 08:17:21 - [info] Node-RED version: v0.20.7 6 Sep 08:17:21 - [info] Node.js version: v8.16.0 6 Sep 08:17:21 - [info] Linux 3.10.0-862.6.3.el7.x86_64 x64 LE 6 Sep 08:17:21 - [info] Loading palette nodes 6 Sep 08:17:22 - [warn] rpi-gpio : Raspberry Pi specific node set inactive 6 Sep 08:17:22 - [warn] rpi-gpio : Cannot find Pi RPi.GPIO python library 6 Sep 08:17:22 - [info] Settings file : /data/settings.js 6 Sep 08:17:22 - [info] Context store : 'default' [module=memory] 6 Sep 08:17:22 - [info] User directory : /data 6 Sep 08:17:22 - [warn] Projects disabled : editorTheme.projects.enabled=false 6 Sep 08:17:22 - [info] Flows file : /data/flows.json 6 Sep 08:17:22 - [info] Creating new flow file 6 Sep 08:17:22 - [warn] --------------------------------------------------------------------- Your flow credentials file is encrypted using a system-generated key. If the system-generated key is lost for any reason, your credentials file will not be recoverable, you will have to delete it and re-enter your credentials. You should set your own key using the 'credentialSecret' option in your settings file. Node-RED will then re-encrypt your credentials file using your chosen key the next time you deploy a change. --------------------------------------------------------------------- 6 Sep 08:17:22 - [info] Server now running at http://127.0.0.1:1880/ 6 Sep 08:17:22 - [info] Starting flows 6 Sep 08:17:22 - [info] Started flows
瀏覽器輸入上面的地址:http://127.0.0.1:1880ide
如今來講安裝就完成了
3. 添加登陸密碼驗證
官網:https://nodered.org/docs/user-guide/runtime/securing-node-red
若是想登陸驗證,設置配置文件settings.js,若是不想設置,能夠跳過。
上面的掛載目錄node-red-data目錄下,會生成settings.js文件,修改adminAuth選項
adminAuth: { type: "credentials", users: [{ username: "admin", password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.", permissions: "*" }] },
註釋完後,須要修改password,改爲你本身的密碼的hash值
進入容器內部
docker exec -ti mynodered bash cd node_modules # 使用下面命令生成密碼hash值 node -e "console.log(require('bcryptjs').hashSync(process.argv[1], 8));" your-password-here
這是我用admin生成的hash值:$2a$08$ADvb5sLmp1N4PWcr27Hfr.3JogbYfsBqbbmoiK14Zoq8Tbv8j91JO
重啓容器:
docker restart mynodered
再訪問瀏覽器就能夠看到登陸畫面輸入密碼了
--------------------------------------------完----------------------------------------------