-
Notifications
You must be signed in to change notification settings - Fork 33
Create an admin configuration API endpoint
Status: Preliminary Notes
Refs: https://stackoverflow.com/questions/41567175/send-data-on-configuration/41567832#41567832
Sometimes a custom node needs to be able to look up additional data at configuration time (e.g. when editing the configuration of a node instance in the admin interface).
The approved way to do this is for the node to register an extra http endpoint to use as an API.
The following is copied from Nick's StackOverflow answer:
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/
.