Hmm, not been a lot of call for those so far so I don't know that I have an example to hand. I need to see if I've got any of the old tests ... ah wait, yes I do have the code, not sure about an accompanying flow.
Here is some code for an API:
/** Example uibuilder instance API file with multiple methods
* Each method will be applied to `<uibInstanceURL>/api/:something` path
* You have to reload Node-RED if you change this file.
* It is best to use named functions as shown because it makes debugging URL paths easier when using the uibindex page.
*/
'use strict'
function apiUse(req, res, next) {
console.log('>> api.js USE >>', req.params)
next()
}
function apiGet(req, res, next) {
res.send('Woo Hoo')
console.log('>> api.js GET >>', req.params)
next()
}
module.exports = {
// Must be a valid ExpressJS URI path. It will be appended to the instance URL.
path: '/api/one/:something',
// route name='use', path='/api/', route=''
use: apiUse,
// route name='bound dispach', path='/api/', route='get:/^\/?$/i'
get: apiGet,
}
It goes into a file with a *.js name in the api folder.
Here is another example:
/** Example uibuilder instance API file with custom apiSetup()
*/
'use strict'
let uibNode, uibMaster
function api3Get(req, res, next) {
let contexts = Object.keys(uibMaster.RED.settings.contextStorage)
contexts = contexts.join(', ')
res.send(
`
<div>
Node ID: ${uibNode.id}, <br>
uibuilder root path: ${uibMaster.nodeRoot}, <br>
Node-RED context stores: [${contexts}]
</div>
<h2>Node-RED Settings</h2>
<pre><code>${JSON.stringify(uibMaster.RED.settings)}</code></pre>
`
)
console.log('>> api3.js GET >>', req.params)
next()
}
module.exports = {
// Must be a valid ExpressJS URI path. It will be appended to the instance URL.
path: '/api/three/',
apiSetup(node, uib) {
//console.log('>> api3Setup >>', node.url, '>> node >>', node, '>> uib >>', uib, '<<<<')
uibNode = node
uibMaster = uib
},
// route name='bound dispach', path='/api/three/', route='get:/^\/?$/i'
get: api3Get,
}
As you can see, the instance api's use a somewhat simplified ExpressJS structure. the uibNode and uibMaster vars are magic variables that are passed from the parent uibuilder node (I think! I can't actually remember now
). Instance API Doc - allowed fn names - you must use the apiSetup function to populate those, you can actually call them anything as you can see in the 2nd example.