嵌入到现有应用中

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

node-red添加到您应用程序package.json文件的模块依赖项中,同时添加您可能需要的任何独立节点依赖项。

以下是将运行时嵌入到更广泛的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