I was going through the node red api documentation and was reading the node-red API-reference section under documentation. So I read @node-red/editor api, specifically httpAdmin- there it was just written that"The Express app used to serve the Node-RED Editor " but there are not any code examples on how to use this api and how it works. I am finding it a little difficult to understand the working of node-red or may be I am reading the wrong way, so I need help here
Perhaps it would help if you described what you are trying to do?
Nick has acknowledged that more documentation is needed for the Node-RED API's but unless more people are prepared to chip in and help, there are only so many hours in the day.
I currently make use of both ExpressJS endpoints in uibuilder if that is of any help.
<script type="text/javascript">
RED.nodes.registerType("createapproval", {
category: "sofos",
color: "#a6bbcf",
defaults: {
name: { value: "" },
approvalgroup: { value: "" },
},
inputs: 1,
outputs: 1,
icon: "file.png",
label: function () {
return this.name || "createapproval";
},
oneditprepare: function () {
$.ajax({
url: "/fetch_groups.bto",
method: "GET",
dataType: "json",
contentType: "application/json",
success: function (response) {
console.log({ response });
response.forEach((group) => {
$("#node-input-approvalgroup").append(`<option value="${group._id}">
${group.name}
</option>`);
});
},
});
},
});
</script>
<script type="text/html" data-template-name="createapproval">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
<label for="node-input-approvalgroup"
><i class="fa fa-tag">Approval Group</i>
</label>
<select id="node-input-approvalgroup" required>
<option value=""></option>
</select>
</div>
</script>
<script type="text/html" data-help-name="createapproval">
<p>A node that create tasks for requested item.</p>
</script>
let O = require("../engine/E");
const ob = new O("dev");
const xe = global.xe;
module.exports = function (RED) {
function CreateApproval(config) {
RED.nodes.createNode(this, config);
var node = this;
const getCtx = node.context().flow.get;
node.on("input", function (msg) {
let groupId = "";
const current = getCtx("current");
if (msg.payload.approvalgroup) {
groupId = msg.payload.approvalgroup;
} else {
groupId = config.approvalgroup;
}
ob.db.findWithOptions(
"pltf_group_member",
{
"group.value": new ob.objectId(groupId),
domain,
},
{},
(err, groupMembers) => {
createApprovals(groupMembers);
}
);
function createApprovals(groupMembers) {
ob.async.eachSeries(
groupMembers,
(member, callback) => {
const approval = {
short_description: current.short_description,
approval_document_collection: current.collection,
approval_document: current._id,
approver: {
label: member.user.label,
id: member.user.id,
},
status: "pending_approval",
};
createRecord(approval, callback);
},
(err) => {
ob.log("done");
}
);
function createRecord(approval, callback) {
ob.db.create(
"approval",
approval,
{
user: { userid: "obto.admin", user_name: "admin" },
domain: current.domain,
},
function (err, doc) {
if (err) {
ob.log(err);
} else {
ob.log(doc);
const process = function (data) {
ob.log(data);
if (
data.status === "approved" ||
data.status === "rejected"
) {
node.send(msg);
}
};
const eventName = `approval.${doc.insertedId}`;
if (ob.em.eventNames().includes(eventName)) {
ob.em.off(eventName, process);
}
ob.em.on(eventName, process);
}
callback();
}
);
}
}
});
}
RED.nodes.registerType("createapproval", CreateApproval);
RED.httpAdmin.post(
"/fetch_groups.bto",
RED.auth.needsPermission("debug.write"),
function (req, res) {
const domain = req.subdomains[0];
ob.db.findWithOptions(
"pltf_group",
{
domain,
},
{},
(err, groups) => {
res.json(groups);
}
);
}
);
};
the above code is the html file and js file for creating a node.
Now you will see that at the end of JS file, there's an api endpoint '/fetch_groups.bto' defined by RED.httpAdmin.post.
I did'nt knew about it and how to use this API to fetch the groups to show in the dropdown in the editor.
And I wrote the whole code in a wrong way and i tried to request an REST API endpoint in my express app like this
app.get(
"/fetch_groups.bto",
function (req, res) {
const domain = req.subdomains[0];
ob.db.findWithOptions(
"pltf_group",
{
domain,
},
{},
(err, groups) => {
res.json(groups);
}
);
}
);
instead of using NODE-RED.
And this is my senior's code not mine
Maybe I lack NODE-RED's understanding of when to use NODE JS and when to use NODE-RED
Thanx
I guess part of the problem is knowing what to look for.
If you know that Node-RED is built on Node.js and uses ExpressJS then you might start with a suitable search which would turn up examples quite quickly and would lead to to the RED.httpAdmin
object. A search for that would certainly lead you to discussions and examples of use.
But yes, you are absolutely right that this is not easily discovered directly from the documentation. If you were willing to write something, the best thing to do would be to engage with Nick on the #docs channel on Slack.
RED.httpAdmin
is, of course the object that most ExpressJS examples would show as app
.
This is another reason for having your own test node. You could walk through the RED
objects properties to discover more about them. Of, of course, you can read through the source code.
If you are going to write custom nodes for Node-RED, it is important of course to understand something about its structure and how it works. Node-RED is a very complex and flexible beast giving us an amazing and versatile platform to work with. But by-and-large, it is very much a Node.js application and should be quite comprehensible if you have Node.js knowledge.
The use of app
is not Node.js - is it merely a common convention used when demonstrating simple tutorials on how to use Express.js (indeed it is a common convention when demonstrating other libraries too such as front-end libraries like VueJS and REACT).
When you are writing the .js
file of a node, you are always using Node.js but also using the API's and conventions built into Node-RED. When you are writing the .html
file of a node, you are NOT using Node.js because that code is loaded into the browser not the runtime, you do however inherit some upstream libraries including jQuery and also a RED object that is different to the runtime RED object.
Thanks a lot for the answer sir. I will try to understand the NODE-RED working first because thats where I got confused.
Is there any easy way to understand the working of NODE-RED or do i have to go through the code?
What in particular are you interested in understanding?
If your goal is to create your own custom nodes, then you don't need to understand all of the inner workings of the runtime.
If you can explain your interest, we can help point you in the right direction.
Well sir as I told you in the example above, the way I got the code wrong....I don't want to do that again.
Just want to write the correct code at correct point or specific functionality. Just want to know how powerful is node red.....want to explore it. Some kind of advanced tutorials to build my normal NODEJS BACKEND with NODE-RED instead of ExpressJS.
Like Serve my static html, css, js files with NODE-RED
Make GET, POST request from my website to my server created on NODE-RED.
In a nutshell, I want to my normal NODEJS BACKEND to be completely in NODERED
If I am understanding you, then node-red is enough already. Without developing a custom node there is not a lot you can't achieve. Use the http nodes to make endpoints. Use http request nodes for accessing other APIs. Use the function nodes, switch nodes, change nodes for custom logic processing. There's databases nodes and MQTT nodes for reading/writing external data.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.