嵌入到现有应用

可以将 Node-RED 嵌入到更大的应用程序中。一个典型的场景是,您使用 Node-RED 生成数据流,并希望在同一个应用程序的 Web 仪表板上显示这些数据流。

在您的应用程序的 package.json 中,将 node-red 添加到模块依赖项中,以及您可能拥有的任何单独的节点依赖项。

以下是将运行时嵌入到更广泛的 Express 应用程序中的最小示例。

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:"/home/nol/.nodered/",
    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();

当使用这种方法时,Node-RED 中包含的 settings.js 文件将不会被使用。相反,设置将传递给 RED.init 调用,如上所示。

此外,以下设置将被忽略,因为它们由您自行配置 Express 实例:

  • uiHost
  • uiPort
  • httpAdminAuth
  • httpNodeAuth
  • httpStatic
  • httpStaticAuth
  • https