发送配置数据

Send data on configuration

我想向配置中的节点发送异步数据。我想要 执行 SQL 请求以在 .

中列出一些数据

或者,二进制文件可以每 x 分钟请求一次数据库并创建一个 每个节点将在创建时使用的缓存,这将删除异步 代码的一部分,即使它不再是 "live updated".

事实上,我被卡住了,因为我创建了查询并将其添加如下:

module.exports = function(RED) {
    "use strict";
    var db = require("../bin/database")(RED);

    function testNode(n) {
        // Create a RED node
        RED.nodes.createNode(this,n);

        // Store local copies of the node configuration (as defined in the 
.html
        var node = this;
        var context = this.context();


        this.on('input', function (msg) {
            node.send({payload: true});
        });
    }

    RED.nodes.registerType("SQLTEST",testNode);
}

但我不知道如何将数据传递给配置节点。我想到了 Socket.IO 去做,但是,这是个好主意吗?可以吗?你知道任何解决方案吗?

Node-RED 中使用的标准模型是让节点注册自己的管理 http 端点,可以用来查询它需要的信息。您可以在串行节点上看到这一点。

串行节点编辑对话框列出了当前连接的串行设备供您选择。

节点在此处注册管理端点:https://github.com/node-red/node-red-nodes/blob/83ea35d0ddd70803d97ccf488d675d6837beeceb/io/serialport/25-serial.js#L283

RED.httpAdmin.get("/serialports", RED.auth.needsPermission('serial.read'), function(req,res) {
    serialp.list(function (err, ports) {
        res.json(ports);
    });
});

要点:

  • 选择一个 url 命名空间到你的节点类型 - 这避免了冲突
  • needsPermission 中间件用于确保只有经过身份验证的用户才能访问端点。许可的格式应为 <node-type>.read.

它的编辑对话框然后从这里查询那个端点:https://github.com/node-red/node-red-nodes/blob/83ea35d0ddd70803d97ccf488d675d6837beeceb/io/serialport/25-serial.html#L240

$.getJSON('serialports',function(data) {
    //... does stuff with data
});

要点:

  • 这里的 url 不能以 / 开头。这确保了请求是相对于编辑器服务的位置发出的——你不能假设它是从 /.
  • 服务的