Inject 和 Debug 节点
Inject 和 Debug 节点
本节介绍 Node-RED 中最常用的两个节点——Inject 和 Debug。学习完成后,您将能够:
- 使用 Inject 节点触发 Flow 和定时任务
- 使用 Debug 节点监控和排查消息
- 配置 Inject 节点的各种触发方式
- 掌握 Debug 消息的解读方法
Inject Node
Section titled “Inject Node”Inject 节点用于手动或自动触发 Flow 执行:
[Inject Node] ──→ [Function] ──→ [Output]
触发方式:├─ 手动 (点击按钮)├─ 定时 (间隔重复)├─ 计划 (指定时间)└─ 启动时 (部署后自动)| 配置项 | 选项 | 说明 |
|---|---|---|
| Payload | String, Number, Boolean, JSON, Timestamp, Flow/Global | 发送的消息内容 |
| Topic | 字符串 | 可选的主题标识 |
| Repeat | Interval / Interval between times | 定时重复 |
| Once | At startup / After X seconds | 启动后自动触发 |
| Name | 自定义名称 | 节点标识 |
场景 1: 手动测试
# 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 Node
Section titled “Debug Node”Debug 节点用于显示消息内容和 Flow 运行状态:
[Any Node] ──→ [Debug Node] ──→ Debug Panel ↓ Sidebar Output| 配置项 | 说明 |
|---|---|
| Output | msg.payload / entire message / JSONata expression |
| To | Debug sidebar / system console / both |
| Name | 自定义标识名称 |
| Active | 启用/禁用调试输出 |
Debug Sidebar 功能
Section titled “Debug Sidebar 功能”┌──────────────────────────────────────┐│ 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] │└──────────────────────────────────────┘Debug 使用场景
Section titled “Debug 使用场景”场景 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"Common Patterns
Section titled “Common Patterns”1. Inject + Debug 测试链
Section titled “1. Inject + Debug 测试链”[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;2. 定时 Inject 轮询
Section titled “2. 定时 Inject 轮询”// Inject 每 10 秒触发// Function 调用 HTTP APIvar http = global.get("http");msg.payload = await http.get("http://api.example.com/status");return msg;3. 条件 Debug
Section titled “3. 条件 Debug”[Inject] → [Switch] ──→ [Debug: Active Flow] │ └──→ [Debug: Other Flow]✅ 推荐做法:
- 开发阶段多用 Debug 节点监控数据
- 为 Debug 设置有意义的名称
- 生产环境禁用或移除 Debug 节点
- 使用
node.warn()在 Function 中输出调试信息 - 利用 Inject 的定时功能模拟周期性数据
❌ 避免做法:
- 生产环境留下大量 Debug 节点
- Debug 节点不命名(默认命名混淆)
- 输出整个消息对象(如果不是必需)
- 忘记禁用 Inject 的重复触发
Practice Exercise
Section titled “Practice Exercise”创建一个简单的测试 Flow:
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. 部署并观察输出Summary
Section titled “Summary”- Inject 节点是 Flow 的触发入口,支持手动、定时和启动触发
- Debug 节点是排错的主要工具,实时显示消息内容
- Inject + Debug 组合是快速测试 Flow 的最佳方式
- 生产环境应禁用或移除 Debug 节点
- Function 节点中的
node.warn()提供额外的调试能力