Node.js script
If the standard nodes in IoT Platform (IoT Studio) are insufficient, use the Node.js script node to write custom JavaScript logic. This node supports Node.js v6.10.
Coding
In the Nodes section of the business logic editing page, select a function node to configure a business flow. The Node.js script configuration page is described as follows.
Node nameFull screenSaveExtension library managementCoding guidelines- Using dynamic parameters
Your script can access data from other nodes by using the following built-in variables:
- The
payloadobject contains the output data from the previous node. Access a specific value using the formatpayload.key.For example,
payload.productKeyaccesses the value from an output like{productKey: 'value'}. - The
queryobject contains data from the initial input node of the flow, such as parameters from an HTTP request or data from a device trigger. Access a specific value using the formatquery.parameterName.For example,
query.deviceNameaccesses the value of the deviceName input parameter from an API request node. - The
nodeobject contains the output data from any specific node in the flow, referenced by its ID (node.node_ID). Access a specific value using the formatnode.node_ID.key.For example,
node.node_39***0.pageNumaccesses the value of pageNum from the output of the node with the IDnode_39***0.
To access nested properties within these parameters, use the following format:
- Use
payload.props.PM10.valueto access the value of the PM10 property within the props object from the previous node. - Use
query.deviceContext.deviceNameto access the deviceName variable from the deviceContext object in the first node's output.
- The
- Generating log output
Use
console.logto generate log output, which appears in the debugging information panel. For example:let name = 'Jack'; console.log('Hello', name);
Limitations
| Item | Description |
| Date | The service orchestration runs on Alibaba Cloud Function Compute, which uses UTC time. Therefore, when using the Date object, be aware of the difference between your local time zone and UTC. |
| NPM library |
The script node includes several built-in NPM libraries, such as aliyun-api-gateway, axios, lodash, moment, and uuid, which you can import directly using require(). To install third-party libraries, open the extension library management panel, search for a supported NPM library, and click Install. After installation, import the library into your script using require(). For more details, see the "Using external extension libraries" section. |
| Variables | Variable names must conform to the naming conventions of ECMAScript 2015 strict mode. Do not define variables that contain circular references in your script. Do not use the following reserved keywords as variable names: abstract, boolean, break, byte, case, catch, char, class, continue, const, debugger, default, delete, do, double, else, enum, export, extends, false, finally, for, function, goto, if, import, implements, in, instance, of, int, interface, let, long, native, new, null, package, private, protected, public, return, short, static, super, switch, synchronized, this, throw, throws, transient, try, type of, var, void, volatile, while, with, yield |
Using external extension libraries
You can extend the functionality of your Node.js script by installing supported external NPM libraries.
- In the node configuration panel, click extension library management. In the dialog box that appears, search for an NPM package by name. Search results are displayed in a list that includes the Available Libraries, Version, Description, and Actions columns. Each library provides View Document and Install actions.
- Find your desired library and click Install.
After installation, the library appears in the Installed Libraries list.
- In your code, import the library using require().
/** * @param {Object} payload The output from the previous node. * @param {Object} node The output from a specific node. * @param {Object} query The output from the first node in the service flow. * @param {Object} context { appKey, appSecret } */ module.exports = async function(payload, node, query, context) { const uuid = require('uuid'); const uuidStr = uuid.v1(); return uuidStr; }
Code example
/**
* @param {Object} payload The output from the previous node.
* @param {Object} node The output from a specific node.
* @param {Object} query The output from the first node in the service flow.
*/
module.exports = function(payload, node, query) {
database = [
["A", 11, 111],
["B", 22, 222],
["C", 33, 333],
["D", 44, 444],
["E", 55, 555],
["F", 11, 111],
["G", 22, 222],
["H", 33, 333],
["I", 44, 444],
["J", 55, 555],
["K", 11, 111],
["L", 22, 222],
["M", 33, 333],
["N", 44, 444],
["O", 55, 555],
];
let arr = [];
for (let i = 0; i < query.column; i++) {
arr[i] = database[i];
}
/**
* The returned 'arr' becomes the payload for the next node.
* It is a two-dimensional array containing a number of entries
* specified by the 'query.column' input parameter.
*/
return arr;
}