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

**URL:** <https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502>\
**Category:** General\
**Created:** [23 June 2019 08:07 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502 "2019-06-23T08:07:21Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![gn01868184](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@gn01868184](https://discourse.nodered.org/u/gn01868184)\
**Post date:** [23 June 2019 08:07 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/1 "2019-06-23T08:07:21Z")

</div>

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**

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 June 2019 08:15 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/2 "2019-06-23T08:15:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 June 2019 09:02 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/3 "2019-06-23T09:02:44Z")

</div>

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...

```auto
"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](https://nodered.org/docs/creating-nodes/first-node)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 June 2019 10:19 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/4 "2019-06-23T10:19:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 June 2019 10:41 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/5 "2019-06-23T10:41:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 June 2019 10:50 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/6 "2019-06-23T10:50:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gn01868184](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@gn01868184](https://discourse.nodered.org/u/gn01868184)\
**Post date:** [24 June 2019 11:21 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/7 "2019-06-24T11:21:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [25 June 2019 15:31 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/8 "2019-06-25T15:31:17Z")

</div>

> [@TotallyInformation](#):
>
> I find the .html file very confusing

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](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/core/60-link.js) and[.html](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/core/60-link.html)).

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 June 2019 19:08 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/9 "2019-06-25T19:08:28Z")

</div>

> [@drmibell](#):
>
> I'm not finding the .html file at all...

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

> [@drmibell](#):
>
> Once in a while, several nodes will share code or require simultaneous modification, so that keeping them in the same file can be convenient

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 `require`d 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.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [25 June 2019 19:19 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/10 "2019-06-25T19:19:23Z")

</div>

> [@TotallyInformation](#):
>
> It is the file that defines the Node-RED admin ui panel for your node

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 🤭.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 June 2019 20:56 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/11 "2019-06-25T20:56:22Z")

</div>

> [@drmibell](#):
>
> Perhaps the developers could give us a nice IDE in their spare time

Haha, I would prefer an extension for VScode personally 🙂

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 June 2019 07:06 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/12 "2019-06-26T07:06:24Z")

</div>

> [@TotallyInformation](#):
>
> A future improvement...

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 June 2019 07:57 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/13 "2019-06-26T07:57:18Z")

</div>

> [@dceejay](#):
>
> a future option

Of course I meant that! How could you think otherwise 😄

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 June 2019 20:08 UTC](https://discourse.nodered.org/t/how-create-a-node-two-function-in-a-node/12502/14 "2019-06-26T20:08:35Z")

</div>

> [@TotallyInformation](#):
>
> In fact, I find the .html file very confusing just in itself and it definitely isn't IDE friendly

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.
