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功能了
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章