发送configuration数据

我想在configuration上发送asynchronous数据到节点。 我想要执行一个SQL请求来列出一些数据。

  • 在创build节点时,执行服务器端function
  • 完成后,callback将数据发送到节点configuration
  • 在节点configuration上,当收到数据时,列表被创build

或者,二进制可以每x分钟请求一次数据库,并创build每个节点在创build时使用的caching,这将删除代码的asynchronous部分,即使它不再“实时更新”。

事实上,我卡住,因为我创build了查询,并添加如下:

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); } 

但是我不知道如何将数据传递给configuration节点。 我想到了Socket.IO,但是,这是一个好主意,它是可用的吗? 你知道任何解决scheme吗?

Node-RED中使用的标准模型是节点注册自己的admin http端点,可用于查询所需的信息。 您可以通过串行节点看到这一点。

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

节点在这里注册pipe理端点: 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); }); }); 

关键点:

  • select一个名称空间为您的节点types的url – 这可以避免冲突
  • needsPermission中间件就是为了确保只有通过身份validation的用户才能访问端点。 权限应该是<node-type>.read的forms。

其编辑对话框然后从这里查询该端点: 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不能以/开头。 这可以确保请求是相对于编辑器的任何地方进行的 – 你不能假设它是从/