Dynamically add new node

#1

I have an application with embedded node-red. In the application the user register some math operations. After that, I want to create a new node and add it to the palette. There is any way to do that?

I've tried to create a new folder with 3 files on it (.js / .json / .html).
Then, using child_process, run npm link node-name and finally do a POST request.

            request.post('http://localhost:3000/red/nodes/'
            ,  { json: { module: `npm module node-red-contrib-${MathOperation}` } }        
            , function(error, response, body){
                if(error) console.log(`ERROR??? ${error}`)
                console.log(`response`, response);
                console.log(`body`, body);
            });

where '/red' is the admin-root as seen:

var settings = {
  httpAdminRoot: "/red",
  httpNodeRoot: "/api",
  userDir: "myUserDir"
  functionGlobalContext: { }    // enables global context
};
#2

The module parameter of the request should just be the module name that will get passed to the npm install command.

#4

Hum... I've made this change but still no success.

request.post('http://localhost:3000/red/nodes/'
            , { json: { module: `node-red-contrib-newoNE` } }        
            , function(error, response, body){
                if(error) console.log(`ERROR??? ${error}`)
                console.log(`body`, body);
            });

Now I'm receiving the following warn:

27 Nov 14:25:45 - [info] Installing module: node-red-contrib-newoNE, version: latest
27 Nov 14:25:48 - [warn] Installation of module node-red-contrib-newoNE failed: module not found

where newoNE is my local new node. I don't know if it helps but bellow are the 3 files I've mentioned before:


//----------------------- Package.JSON -----------------------

{
	"name": "node-red-contrib-newoNE",
	"version": "0.0.1",
	"main": "newoNE.js",
	"description": "A sample node math operator for node-red",
	"dependencies": {},
	"keywords": [
		"node-red"
	],
	"node-red": {
		"nodes": {
			"newoNE": "newoNE.js"
		}
	}
}



//----------------------- NODE.js -----------------------

module.exports = function(RED) {
  function LowerCaseNodeCustom(config) {
      RED.nodes.createNode(this,config);
      var node = this;
      node.on('input', function(msg) {
          msg.payload = msg.payload.toLowerCase() + "This_is_a_new_math_operator";
          node.send(msg);
      });
  }
  RED.nodes.registerType("newoNE", LowerCaseNodeCustom);
}



//----------------------- NODE.html -----------------------

<script type="text/javascript">
    RED.nodes.registerType('newoNE',{
        category: 'function',
        color: '#00ff00',
        defaults: {
            name: {value:""}
        },
        inputs:1,
        outputs:1,
        icon: "file.png",
        label: function() {
            return this.name||"newoNE";
        }
    });
</script>
<script type="text/x-red" data-template-name="newoNE">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
</script>
<script type="text/x-red" data-help-name="newoNE">
    <p>A simple node that converts the message payloads into all lower-case characters and add This_is_a_new_math_operator</p>
</script>

#5

Have you published your module to npm or are the files locally on disk?

If they are locally on disk, then the module parameter you pass in the post request must be the path to wherever your module is located on disk.

#6

Thanks it is working now! Just to let you know I'm using a local module.