JavaScript 文件

节点 .js 文件定义了节点的运行时行为。

节点构造函数

节点由一个构造函数定义,该函数可用于创建节点的新实例。该函数在运行时注册,以便当相应类型的节点部署在流中时可以调用它。

该函数被传递一个对象,其中包含流编辑器中设置的属性。

它必须做的第一件事是调用 RED.nodes.createNode 函数来初始化所有节点共享的功能。之后,就是节点特定的代码。

function SampleNode(config) {
    RED.nodes.createNode(this,config);
    // node-specific code goes here

}

RED.nodes.registerType("sample",SampleNode);

接收消息

节点在 input 事件上注册一个监听器,以接收来自流中上游节点的消息。

Node-RED 1.0 引入了一种新的监听器样式,允许节点在处理完消息后通知运行时。这为监听器函数增加了两个新参数。需要注意确保节点仍然可以安装在 Node-RED 0.x 中,因为它不使用这种新的监听器样式。

this.on('input', function(msg, send, done) {
    // do something with 'msg'

    // Once finished, call 'done'.
    // This call is wrapped in a check that 'done' exists
    // so the node will work in earlier versions of Node-RED (<1.0)
    if (done) {
        done();
    }
});

错误处理

如果节点在处理消息时遇到错误,它应该将错误的详细信息传递给 done 函数。

这将触发同一选项卡上存在的任何 Catch 节点,允许用户构建流来处理错误。

同样,如果节点安装在不提供 done 函数的 Node-RED 0.x 中,则需要小心。在这种情况下,它应该使用 node.error

let node = this;
this.on('input', function(msg, send, done) {
    // do something with 'msg'

    // If an error is hit, report it to the runtime
    if (err) {
        if (done) {
            // Node-RED 1.0 compatible
            done(err);
        } else {
            // Node-RED 0.x compatible
            node.error(err, msg);
        }
    }
});

发送消息

如果节点位于流的开始,并根据外部事件生成消息,它应该使用 Node 对象上的 send 函数

var msg = { payload:"hi" }
this.send(msg);

如果节点想要在 input 事件监听器内部,响应接收到的消息发送,它应该使用传递给监听器函数的 send 函数

let node = this;
this.on('input', function(msg, send, done) {
    // For maximum backwards compatibility, check that send exists.
    // If this node is installed in Node-RED 0.x, it will need to
    // fallback to using `node.send`
    send = send || function() { node.send.apply(node,arguments) }

    msg.payload = "hi";
    send(msg);

    if (done) {
        done();
    }
});

如果 msg 为 null,则不发送消息。

如果节点响应接收到的消息发送消息,它应该重用接收到的消息而不是创建新的消息对象。这确保了消息上现有的属性为流的其余部分保留。

多输出

如果节点有多个输出,可以将消息数组传递给 send,每个消息都发送到相应的输出。

this.send([ msg1 , msg2 ]);

多条消息

通过在此数组中传递消息数组,可以将多条消息发送到特定输出

this.send([ [msgA1 , msgA2 , msgA3] , msg2 ]);

关闭节点

每当部署新流时,现有节点都会被删除。如果其中任何一个需要在此发生时清理状态(例如断开与远程系统的连接),它们应该在 close 事件上注册一个监听器。

this.on('close', function() {
    // tidy up any state
});

如果节点需要执行任何异步工作来完成清理,则注册的监听器应该接受一个参数,该参数是一个在所有工作完成后要调用的函数。

this.on('close', function(done) {
    doSomethingWithACallback(function() {
        done();
    });
});

自 Node-RED 0.17 起

如果注册的监听器接受两个参数,则第一个参数将是一个布尔标志,指示节点是因为被完全删除而关闭,还是仅仅被重新启动。如果节点被禁用,它也将设置为 *true*。

this.on('close', function(removed, done) {
    if (removed) {
        // This node has been disabled/deleted
    } else {
        // This node is being restarted
    }
    done();
});

超时行为

自 Node-RED 0.17 起

在 Node-RED 0.17 之前,运行时会无限期地等待 done 函数被调用。如果节点未能调用它,这将导致运行时挂起。

在 0.17 及更高版本中,如果节点耗时超过 15 秒,运行时将超时节点。将记录错误,并且运行时将继续运行。

日志事件

如果节点需要将内容记录到控制台,它可以使用以下函数之一

this.log("Something happened");
this.warn("Something happened you should know about");
this.error("Oh no, something bad happened");

// Since Node-RED 0.17
this.trace("Log some internal detail not needed for normal operation");
this.debug("Log something more details for debugging the node's behaviour");

warnerror 消息也会发送到流编辑器调试选项卡。

设置状态

在运行时,节点能够与编辑器 UI 共享状态信息。这是通过调用 status 函数完成的

this.status({fill:"red",shape:"ring",text:"disconnected"});

状态 API 的详细信息可以在这里找到。

自定义节点设置

节点可能希望在用户的 settings.js 文件中公开配置选项。

任何设置的名称必须符合以下要求

  • 名称必须以相应的节点类型为前缀。
  • 设置必须使用驼峰命名法 - 更多信息请参阅下文。
  • 节点不得要求用户设置它 - 它应该有一个合理的默认值。

例如,如果节点类型 sample-node 想要公开一个名为 colour 的设置,则设置名称应为 sampleNodeColour

在运行时,节点可以引用设置,如 RED.settings.sampleNodeColour

向编辑器公开设置

自 Node-RED 0.17 起

在某些情况下,节点可能希望向编辑器公开设置的值。如果是这样,节点必须在调用 registerType 时注册设置

RED.nodes.registerType("sample",SampleNode, {
    settings: {
        sampleNodeColour: {
            value: "red",
            exportable: true
        }
    }
});
  • value 字段指定设置应采用的默认值。
  • exportable 告诉运行时将设置提供给编辑器。

与运行时一样,节点可以在编辑器中将设置引用为 RED.settings.sampleNodeColour

如果节点尝试注册不符合命名要求的设置,则会记录错误。