Node-RED Node-RED 使用指南:23:嵌入 Node.js 应用

NodeRED · 2020年08月27日 · 57 次阅读
本帖已被设为精华帖!

Node-RED 可以独立运行,也可以直接嵌入到 Node.js 应用中,这篇文章以具体的示例来进行说明。

环境说明

操作系统

liumiaocn:~ liumiao$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.15.2
BuildVersion: 19C57
liumiaocn:~ liumiao$

node 版本

liumiaocn:~ liumiao$ node -v
v10.15.3
liumiaocn:~ liumiao$ npm -v
6.4.1
liumiaocn:~ liumiao$

Node-RED 版本

liumiaocn:~ liumiao$ node-red -h
Node-RED v1.0.4
Usage: node-red [-v] [-?] [--settings settings.js] [--userDir DIR]
[--port PORT] [--title TITLE] [--safe] [flows.json]

Options:
-p, --port PORT port to listen on
-s, --settings FILE use specified settings file
--title TITLE process window title
-u, --userDir DIR use specified user directory
-v, --verbose enable verbose output
--safe enable safe mode
-?, --help show this help

Documentation can be found at http://nodered.org
liumiaocn:~ liumiao$

代码准备

准备如下基于 express 的示例代码,将本地的 node-red 嵌入到此应用之中

liumiaocn:nodered liumiao$ ls
sample.js
liumiaocn:nodered liumiao$ cat sample.js
var http = require('http');
var express = require("express");
var RED = require("node-red");

// Create an Express app
var app = express();

// Add a simple route for static content served from 'public'
app.use("/",express.static("public"));

// Create a server
var server = http.createServer(app);

// Create the settings object - see default settings.js file for other options
var settings = {
httpAdminRoot:"/red",
httpNodeRoot: "/api",
userDir:"/Users/liumiao/.node-red",
functionGlobalContext: { } // enables global context
};

// Initialise the runtime with a server and settings
RED.init(server,settings);

// Serve the editor UI from /red
app.use(settings.httpAdminRoot,RED.httpAdmin);

// Serve the http nodes UI from /api
app.use(settings.httpNodeRoot,RED.httpNode);

server.listen(8000);

// Start the runtime
RED.start();
liumiaocn:nodered liumiao$

注意事项:/Users/liumiao/.node-red 请修改为自己的本地 Node-RED 的相应目录。

依赖准备

使用如下命令安装所需依赖

执行命令:npm install express node-red

liumiaocn:nodered liumiao$ npm install express node-red
...省略
+ express@4.17.1
+ node-red@1.0.4
updated 2 packages in 3.505s
liumiaocn:nodered liumiao$

启动服务

使用如下命令启动服务,因为此 Node.js 服务本身没有添加功能,所以看起来似乎就是 Node-RED,但是我们知道这是我们使用 node 在 8000 启动的新的服务

liumiaocn:nodered liumiao$ node sample.js
11 Mar 06:21:28 - [info]

Welcome to Node-RED
===================

11 Mar 06:21:28 - [info] Node-RED version: v1.0.4
11 Mar 06:21:28 - [info] Node.js version: v10.15.3
11 Mar 06:21:28 - [info] Darwin 19.2.0 x64 LE
11 Mar 06:21:28 - [info] Loading palette nodes
11 Mar 06:21:28 - [info] Context store : 'default' [module=memory]
11 Mar 06:21:28 - [info] User directory : /Users/liumiao/.node-red
11 Mar 06:21:28 - [warn] Projects disabled : set editorTheme.projects.enabled=true to enable
11 Mar 06:21:28 - [info] Flows file : /Users/liumiao/.node-red/flows_liumiaocn.json
11 Mar 06:21:28 - [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.
---------------------------------------------------------------------

11 Mar 06:21:28 - [info] Starting flows
11 Mar 06:21:28 - [info] Started flows

使用/red 的 URL 即可在 8000 端口确认在此 Node.js 应用中嵌入的 Node-RED 功能了

来源:https://blog.csdn.net/liumiaocn/category_9774438.html

NodeRED 官网:https://nodered.org/

NodeREDGITHUB:https://github.com/node-red

NodeRED 英文社区:https://discourse.nodered.org/

NodeRED 中文社区:https://www.iotschool.com/topics/node81

NodeRED 中文社区 QQ 群:8604461

iotschool

sds 将本帖设为了精华贴 08月27日 21:39
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册