Skip to content

Instantly share code, notes, and snippets.

@sholloway
Last active March 8, 2019 14:52
Show Gist options
  • Save sholloway/c025cfe2202b58f945943d4c83aeb4c6 to your computer and use it in GitHub Desktop.
Save sholloway/c025cfe2202b58f945943d4c83aeb4c6 to your computer and use it in GitHub Desktop.
Node-RED Custom Node Template
<script type="text/javascript">
RED.nodes.registerType('NODE_NAME',{
category: 'The Category',
color: '#C0DEED',
defaults: {
},
inputs: 1,
outputs: 1,
icon: "file.png",
label: function() {
return this.name || "NODE_NAME";
}
});
</script>
<script type="text/x-red" data-template-name="NODE_NAME">
<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="NODE_NAME">
<p>Description of the NODE_NAME node.</p>
</script>
function NodeConstruction(RED) {
const NODE_NAME = 'TODO: Set a node name that can be logged.';
function CustomNode(config) {
RED.nodes.createNode(this,config);
let node = this;
node.on('input', function(msg) {
node.status({fill:"blue",shape:"ring",text:"working..."})
if (invalidMsg(msg)){
//TODO: Build up custom error message.
node.status({fill:"red",shape:"dot",text:"failed"});
node.error('errorMsg',msg);
}
//TODO: Add custom processing here
node.status({fill:"green",shape:"dot",text:"done"});
node.send(msg);
});
/**
* Verifies that the required parameters are all present in the message payload.
* @returns {Boolean} Returns true if a required parameter is invalid.
*/
function invalidMsg(msg){
// Note: Keep in mind that the typeof null is 'object'.
//TODO: Add input validation here.
return typeof msg.payload === 'undefined' || msg.payload === null ;
}
// Nodes are closed when a new flow is deployed.
node.on('close', function(done){
return done();
});
}
let nodeDefinition = {
};
RED.nodes.registerType("NODE_NAME", CustomNode, nodeDefinition);
}
module.exports = NodeConstruction;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment