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

在这里插入图片描述
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
...省略
+ [email protected]
+ [email protected]
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功能了
在这里插入图片描述

发布了1133 篇原创文章 · 获赞 1357 · 访问量 410万+

猜你喜欢

转载自blog.csdn.net/liumiaocn/article/details/104788664