跳转到内容

Inject 和 Debug 节点

Inject 和 Debug 节点

本节介绍 Node-RED 中最常用的两个节点——Inject 和 Debug。学习完成后,您将能够:

  • 使用 Inject 节点触发 Flow 和定时任务
  • 使用 Debug 节点监控和排查消息
  • 配置 Inject 节点的各种触发方式
  • 掌握 Debug 消息的解读方法

Inject 节点用于手动或自动触发 Flow 执行:

[Inject Node] ──→ [Function] ──→ [Output]
触发方式:
├─ 手动 (点击按钮)
├─ 定时 (间隔重复)
├─ 计划 (指定时间)
└─ 启动时 (部署后自动)
配置项选项说明
PayloadString, Number, Boolean, JSON, Timestamp, Flow/Global发送的消息内容
Topic字符串可选的主题标识
RepeatInterval / Interval between times定时重复
OnceAt startup / After X seconds启动后自动触发
Name自定义名称节点标识

场景 1: 手动测试

Terminal window
# 1. 拖入 Inject 节点
# 2. 设置 Payload 为测试数据
# 3. 点击按钮发送
# 4. 观察后续节点处理

场景 2: 定时任务

// 配置: 每 5 分钟执行一次
// Repeat: Interval
// Interval: 300 seconds
// 结合 Function 节点实现定时查询
var now = new Date();
msg.payload = {
timestamp: now.toISOString(),
action: "check_devices"
};
return msg;

场景 3: 系统初始化

// 配置: 启动时执行一次
// Once: At startup
// Once Delay: 5 seconds
// 用于初始化全局变量
var initialStatus = {
system: "starting",
timestamp: Date.now()
};
context.global.systemStatus = initialStatus;
msg.payload = initialStatus;
return msg;

Debug 节点用于显示消息内容和 Flow 运行状态:

[Any Node] ──→ [Debug Node] ──→ Debug Panel
Sidebar Output
配置项说明
Outputmsg.payload / entire message / JSONata expression
ToDebug sidebar / system console / both
Name自定义标识名称
Active启用/禁用调试输出
┌──────────────────────────────────────┐
│ Debug Messages │
├──────────────────────────────────────┤
│ [Filter...] │ ← 搜索过滤
├──────────────────────────────────────┤
│ ● 10:30:25.123 msg.payload │
│ ├── topic: "sensor/temperature" │
│ ├── payload: 25.3 │
│ └── source: flow: "test" │
│ │
│ ● 10:30:20.456 msg.payload │
│ └── ... │
│ │
│ [Count: 42] [Clear] [Pin] [Copy] │
└──────────────────────────────────────┘

场景 1: 监控数据流

Flow 结构:
[MQTT In] → [Function: 解析] → [InfluxDB Out]
[Debug: 原始数据]
[Debug: 处理后数据]

场景 2: 调试 Function 节点

// 在 Function 节点中使用 node.warn() 调试
function processData(msg) {
node.warn("Input payload: " + JSON.stringify(msg.payload));
var result = msg.payload * 2;
node.warn("Output value: " + result);
msg.payload = result;
return msg;
}

场景 3: 追踪错误

// 捕获并输出错误信息
try {
var data = JSON.parse(msg.payload);
msg.payload = data.temperature;
return msg;
} catch (e) {
node.error("Parse error: " + e.message, msg);
return null;
}
// Debug 面板显示:
// "Parse error: Unexpected token ' in JSON at position 1"
[Inject: 25.3] → [Function: 处理数据] → [Debug: 结果]
// Function 节点代码
if (msg.payload > 30) {
msg.payload = { alert: true, value: msg.payload };
} else {
msg.payload = { alert: false, value: msg.payload };
}
return msg;
// Inject 每 10 秒触发
// Function 调用 HTTP API
var http = global.get("http");
msg.payload = await http.get("http://api.example.com/status");
return msg;
[Inject] → [Switch] ──→ [Debug: Active Flow]
└──→ [Debug: Other Flow]

推荐做法:

  • 开发阶段多用 Debug 节点监控数据
  • 为 Debug 设置有意义的名称
  • 生产环境禁用或移除 Debug 节点
  • 使用 node.warn() 在 Function 中输出调试信息
  • 利用 Inject 的定时功能模拟周期性数据

避免做法:

  • 生产环境留下大量 Debug 节点
  • Debug 节点不命名(默认命名混淆)
  • 输出整个消息对象(如果不是必需)
  • 忘记禁用 Inject 的重复触发

创建一个简单的测试 Flow:

Terminal window
1. Inject Node 配置:
- Payload: number
- Value: 25
- Repeat: every 5 seconds
2. Function Node:
if (msg.payload > 30) {
msg.payload = "HIGH: " + msg.payload;
} else {
msg.payload = "LOW: " + msg.payload;
}
return msg;
3. Debug Node:
- Output: msg.payload
- Name: "Processed Data"
4. 部署并观察输出
  1. Inject 节点是 Flow 的触发入口,支持手动、定时和启动触发
  2. Debug 节点是排错的主要工具,实时显示消息内容
  3. Inject + Debug 组合是快速测试 Flow 的最佳方式
  4. 生产环境应禁用或移除 Debug 节点
  5. Function 节点中的 node.warn() 提供额外的调试能力