Send data on configuration

為{幸葍}努か 提交于 2019-12-04 19:43:41

The standard model used in Node-RED is for the node to register its own admin http endpoint that can be used to query the information it needs. You can see this in action with the Serial node.

The Serial node edit dialog lists the currently connected serial devices for you to pick from.

The node registers the admin endpoint here: 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);
    });
});

Key points:

  • pick a url that is namespaced to your node type - this avoids clashes
  • the needsPermission middleware is there to ensure only authenticated users can access the endpoint. The permission should be of the form <node-type>.read.

Its edit dialog then queries that endpoint from here: https://github.com/node-red/node-red-nodes/blob/83ea35d0ddd70803d97ccf488d675d6837beeceb/io/serialport/25-serial.html#L240

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

Key points:

  • here the url must not begin with a /. That ensures the request is made relative to wherever the editor is being served from - you cannot assume it is being served from /.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!