Node-RED 系列(3)-- Core nodes (Inject/Debug/Function)

The inject, debug and function node are the most commonly used of the Node-RED core nodes.
Core nodes are the ones that are part of the initial Node-RED install.

Inject

Injects a message into a flow either manually or at regular intervals. The message payload can be a variety of types, including strings, JavaScripts objects or the current time.

Outputs

payload
The configured payload of the message.
topic
An optional property that can be configured in the node.

Details

The Inject node can initiate a flow with a specific payload value. The default payload is a timestamp of the current time in millisecs since January 1st, 1970.

The node also supports injecting strings, numbers, booleans, JavaScript objects, or flow/global context values.

By default, the node is triggered manually by clicking on its button within the editor. It can also be set to inject at regular intervals or according to a schedule.

It can also be configured to inject once each time the flows are started.

The maximum Interval that can be specified is about 596 hours/ 24days. However if you are looking at intervals greater than one day you should consider using a scheduler node that can cope with power outages and restarts.
Note: The "Interval between times" and "at a specific time" options use the standard cron system. This means that 20 minutes will be at the next hour, 20 minutes past and 40 minutes past - not in 20 minutes time. If you want every 20 minutes from now - use the "interval" option.

Inject node

image.png
    1. trigger the inject manually
    1. debug messages
    1. topic - display the topic info
    1. payload - display the payload info

Debug

Displays selected message properties in the debug sidebar tab and optionally the runtime log. By default it displays msg.payload, but can be configured to display any property, the full message or the result of a JSONData expression.

Details

The debug sidebar provides a structured view of the messages it is sent, making it easier to understand their structure.
JavaScript objects and arrays can be collapsed and expanded as required. Buffer objects can be displayed as raw data or as a string if possible.

Alongside each message, the debug sidebar includes information about the time the message was received, the node that sent it and the type of the message. Clicking on the source node id will reveal that node within the workspace.

The button on the node can be used to enable or disable its output. It is recommended to disable or remove any Debug nodes that are not being used.

The node can also be configured to send all messages to the runtime log, or to send short (32 characters) to the status text under the debug node.


Debug node

Function

A JavaScript function to run against the messages being received by the node.

The messages are passed in as a JavaScript object called msg.

By convention it will have a msg.payload property containing the body of the message.

The function is expected to return a message object (or multiple message objects), but can choose to return nothing in order to halt a flow.

The Setup tab contains code that will be run whenever the node is started. The Close tab contains node that will be run whenever the node is stopped.

If an promise object is returned from the setup node, input message processing starts after its completion.

Details

See the online documentation for more information on writing functions.
Sending messages
The function can either return the messages it wants to pass on to the next nodes in the flow, or can call node.send(messages).
It can return/send:

  • a single message object - passed to nodes connected to the first output
  • an array of message objects - passed to nodes connected to the corresponding outputs

Note: The setup code is executed during the initialization of nodes. Therefore, if node.send is called in the setup tab, subsequent nodes may not be able to receive the message.
If any element of the array is itself an array of messages, multiple messages are sent to the corresponding output.
If null is returned, either by itself or as an element of the array, no message is passed on.

Logging and Error Handling

To log any information, or report an error, the following functions are available:

  • node.log("Log message")
  • node.warn("Warning")
  • node.error("Error")

The Catch node can also be used to handle errors. To invoke a Catch node, pass msg as a second argument to node.error.

node.error("Error",msg);

Accessing Node Information
In the function block, id and name of the node can be referenced using the following properties:

  • node.id - id of the node
  • node.name - name of the node

Using environment variables
Environment variables can be accessed using env.get("MY_ENV_VAR")

你可能感兴趣的:(Node-RED 系列(3)-- Core nodes (Inject/Debug/Function))