介绍

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是全球最大的开源库生态系统。)

安装

为了方便,采用docker的方式安装,附官网

所有安装方式:https://nodered.org/docs/getting-started/

docker安装方式:https://nodered.org/docs/getting-started/docker

1. 首先拉取镜像(这里使用的最新版本latest):

docker pull nodered/node-red-docker

2. 启动一个容器:

我打算把data文件挂载到主机上,省的每次都去容器内部改配置啥的,查看数据也方便

首先在你当前目录(哪儿都可以)创建一个空文件夹node-red-data,设置权限为777

chmod 777 node-red-data/

当前目录启动容器

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 交互参数的话,启动成功会出现如下文字:

# 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:1880

node-red 安装-风君雪科技博客

现在来说安装就完成了

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

再访问浏览器就可以看到登录画面输入密码了

node-red 安装-风君雪科技博客

 ——————————————–完———————————————-