How create a node ? (two function in a node)

How to create a node have two function?
for example >> the node can at dialog select function1 or function2
Thank~
this is my code
computer.js

module.exports = function(RED) {
    function add(config) {
        RED.nodes.createNode(this,config);
        var node = this;
        this.on('input', function(msg) {
        	var total = msg.payload[0] + msg.payload[1];
        	msg.payload = total;
            node.send(msg);
        });
    }
    function less(config) {
        RED.nodes.createNode(this,config);
        var node = this;
        this.on('input', function(msg) {
        	var total = msg.payload[0] - msg.payload[1];
        	msg.payload = total;
            node.send(msg);
        });
    }
    RED.nodes.registerType("Add-case",add);
    RED.nodes.registerType("Less-case",less);
}

Hi.

Before anyone else mentions it....

When you post code, you prefix it with three of the ` key (that's they key to the left of the number 1) and end it with thee of them also.

That way it looks like this:
this is to show you

As your code starts with computer.js I am going to guess you know more than me about coding.

But I think you will put it in a function node.
You can add more outputs on the function node which may be helpful.
But only ONE input.

But you can get around to that by flagging messages with things like their topic, etc.

Good luck.

Hi as @Trying_to_learn says, please edit your post and put three back ticks ``` on a line above and on a line below your code so it is both formatted correctly and readable.

A Node red custom node js file (in your case computer.js) can contain multiple nodes. For example the official source for nodes tcp in, tcp out and tcp request are all in one file. You also need a matching .html file and lastly, your package.JSON needs to list them both. E.g.

Example showing 3 nodes shares same code file...

"node-red": {
        "nodes": {
            "jimp": "jimp.js",
            "xzing2dDecode": "jimp.js",
            "imageViewer": "jimp.js"
        }
    },

Good place to start is here...
https://nodered.org/docs/creating-nodes/first-node

Personally, I don't recommend putting multiple nodes in 1 file, it is confusing to come back to in the future.

I agree too but it was the OPs question.

My own custom nodes are all in their own files - I find it better to understand what happens where.

Also, @gn01868184 I would recommend naming your js and HTML files appropriately. add.js/add.html is preferable & more meaningful than computer.js.

1 Like

Indeed.

In fact, I find the .html file very confusing just in itself and it definitely isn't IDE friendly. So I now write the code for that file in 3 separate files (the admin UI, the help info and the script) in a sub-folder and have a script that stitches them together. Now I can develop them properly using the full capabilities of VScode.

thanks everyone
I do not have much experience on node-red dev but will try ..

I'm not finding the .html file at all...

I agree with @TotallyInformation and @Steve-Mcl about using separate files in general. Once in a while, several nodes will share code or require simultaneous modification, so that keeping them in the same file can be convenient. A good example of how to do this cleanly is in the core link in and link out nodes (.js and.html).

??? It is the file that defines the Node-RED admin ui panel for your node.

That does make sense when the .js code is as short as the excellent link nodes. It also makes sense in the .html code in the link case as so much of its javascript is common.

However, I would go even further. I separate out common code to a separate file again. uibuilder v2 has two such libraries. 1 that contains common code that requires data from a node instance and probably a reference to RED - uilib.js and one that is generic utilities such as safely reading a JSON file - tilib.js. Each of these is required as necessary.

That is easier for the .js file. But even for the .html file, I would extend my build script - which simply merges 3 files into a template file, I would extend that with a common code file. This makes development soooo much easier because the code is really easy to read.

In fact, I might well do that now you've made me think about it since even though I'm only defining a single node, I do have quite a number of functions that I use to keep the actual admin panel definition smaller and easier to follow.

Understood. I thought you were referring specifically to the OP's .html file, not to NR .html files in general.

You are exactly right: developing nodes is quirky, since both the .js and .html files use NR-specific definitions and structures that make life difficult for standard editors and debuggers. Perhaps the developers could give us a nice IDE in their spare time :face_with_hand_over_mouth:.

Haha, I would prefer an extension for VScode personally :slight_smile:

Or at least a typescript definitions file for the RED library.

A future improvement to the .html file to split it in 3 natively would also be a really nice upgrade.

One persons improvement, is another persons “yet more files to track and understand”. Maybe you mean “a future option...”?

1 Like

Of course I meant that! How could you think otherwise :smile:

In fact, that is what I meant, after all, backwards compatibility is critical.

1 Like

I'd be happy if they were contained in almost anything except a <script> tag so the IDE could understand the contents. Even a custom tag would probably be ok.