1.Creating your first node
nodes are created when a flow is deployed. They may send and receive some messages when the flow is running and be deleted when the next flow is deployed.
They consist of a set of files:
JavaScript files that define node functions,
An html file that defines node properties, edit dialog boxes, and help text.
package.json files are used to package them into an npm module.
Basic files required to create a simple node:
package.json lower-case.js lower-case.html
Test your node in node red
2. Create a simple node
This example shows how to create a node that converts the message payload to all lowercase characters.
Make sure that the node of the current LTS version is installed on the system JS.
Create a directory where you will develop code. In this directory, create the following files:
package.json lower-case.js lower-case.html
2.1package.json
this is node JS module is a standard file used to describe its content.
to generate a standard package JSON file, you can use the command npm init. This will ask a series of questions to help create the initial contents of the file and use reasonable defaults as much as possible.
(process:)
① Press windows+R, enter cmd and open the terminal; ② Navigate to the disk where you want to create a new file; ③ Execute the command npm init
When prompted, name it node red contrib example lower case
The name of lower case can be changed;
after that, the default operation is OK, and then the package will be generated in the corresponding path JSON file;
after generation, you must go to package Add a node red section to the jisn file manually: the purpose is to associate the node red with the following js file. If there are configuration nodes in the future, the configuration node will be added here js file; This tells the runtime which node files the module contains.
"node-red" : { "nodes": { "lower-case": "lower-case.js" } },
2.2lower-case.js
This is a function realization file. The function of the node depends on it (front end and back end):
module.exports = function(RED) { function LowerCaseNode(config) { RED.nodes.createNode(this,config); var node = this; node.on('input', function(msg) { msg.payload = msg.payload.toLowerCase(); node.send(msg); }); } RED.nodes.registerType("lower-case",LowerCaseNode);}
2.3lower-case.html
This is a file edited by the appearance of a node. It depends on what the node looks like html file;
<script type="text/javascript"> RED.nodes.registerType('lower-case',{ category: 'function', color: '#a6bbcf', defaults: { name: {value:""} }, inputs:1, outputs:1, icon: "file.png", label: function() { return this.name||"lower-case"; } });</script> <script type="text/html" data-template-name="lower-case"> <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"> </div></script> <script type="text/html" data-help-name="lower-case"> <p>A simple node that converts the message payloads into all lower-case characters</p></script>
3. Test the node in node red
after creating the basic node module as described above, you can install it into the node red runtime.
To test the node module locally, you can use the NPM install < folder > command. This allows you to develop nodes in a local directory and link them to your local node red installation during development.
(process)
On Windows you would do:
① First locate to Disk C;
② Then execute CD C: \ users \ administrator Node red command;
③ Then execute the npm install F: \ desktop \ create command
F: \ desktop \ create command refers to the path of the created file;
this creates an appropriate symbolic link to the directory (create node) so that node red can discover the node at startup. Just restart node red to get any changes to the node file (instead of executing the npm install... Command every time the file is changed, just restart node red).
it succeeds when there is no problem. Open the terminal and run the node red command; Open the node red website again and you can see that we have added a new function lower case on the left. Then you can create a stream. After deployment and execution, as shown in the figure below, change the uppercase letter to lowercase letter;
it seems that we have succeeded, but we have succeeded. However, we are missing the most important part of the process, the unit test step, which is after the node is created and before running node red.
4. Unit test
to support unit testing, you can use an npm module called node red node test helper. Test helper is a framework built based on the node red runtime, which makes it easier to test nodes.
4.1 using this framework, you can create test flow;
then assert that your node properties and output work as expected. For example, to add a unit test to a lowercase node, you can add the test folder to the node module package, which contains a file named_ spec.js file
(create a test folder in the project file on my desktop, and then create a file in this folder (the file name is * * *_spec.js, such as lower case_spec.js file). Manually enter the code in the file)
var helper = require("node-red-node-test-helper"); var lowerNode = require("../lower-case.js"); describe('lower-case Node', function () { afterEach(function () { helper.unload(); }); it('should be loaded', function (done) { var flow = [{ id: "n1", type: "lower-case", name: "test name" }]; helper.load(lowerNode, flow, function () { var n1 = helper.getNode("n1"); n1.should.have.property('name', 'test name'); done(); }); }); it('should make payload lower case', function (done) { var flow = [{ id: "n1", type: "lower-case", name: "test name",wires:[["n2"]] }, { id: "n2", type: "helper" }]; helper.load(lowerNode, flow, function () { var n2 = helper.getNode("n2"); var n1 = helper.getNode("n1"); n2.on("input", function (msg) { msg.should.have.property('payload', 'uppercase'); done(); }); n1.receive({ payload: "UpperCase" }); }); });});
4.2 add project dependencies to your node
the helper requires node red as a peer dependency, which means it must be installed with the helper itself. To create a unit test for your node project, add this test assistant and node red, as follows: execute the following commands: (open the package.json file in VS Code, ctrl + ` open the terminal, locate the path of the create file (project file) on the desktop, and execute the following commands)
npm install node-red-node-test-helper node-red --save-dev
This will add the help module to your package JSON file:
4.3 add test script to package json
to run the test, you can run it in package. In the script section Add a test script to the JSON file. To run all tests in the test directory_ For example, change the above figure to the following figure:
This will allow you to use the npm test command on the command line.
4.4 finally, open package. In VS Code JSON file
locate the cd to the creat e file on the desktop, and then execute the npm test command to wait for successful execution, as shown in the following figure:
5. End
this is the official end. The computer terminal runs and opens node red. The web side creates a stream, deploys, and executes successfully, as shown in the following figure: