Code Runner Node
The Code Runner is a powerful component in FlowAI that enables users to write and execute JavaScript code for handling complex business logic. This tool empowers you to implement advanced functionalities such as string manipulation, conditional judgments, and loop operations, significantly enhancing workflow flexibility and extensibility. It provides unparalleled freedom in data processing, allowing you to go beyond the limitations of our existing node functionalities.
Node Configuration
Basic Settings
-
Node Name
- Set a descriptive name for the code runner to facilitate identification and management.
- Recommended to use purpose-reflective names like “Data Transformation” or “String Processing”, ensuring team members can quickly understand the node’s functionality.
-
Input Parameters Supports two input methods:
- Manual Input: Directly enter fixed values, suitable for simple scenarios.
- Contextual Reference: Retrieve values from previous node outputs, ideal for complex workflows.
-
Code Editor
- Write JavaScript code in this area with clear logic.
- Must include a
return
statement to pass results to subsequent nodes. - Access input parameters using
input.parameterName
(replace with actual parameter name) for flexible data handling.
Code Format Specifications
The code runner will execute your code wrapped within a function. Follow this format:
// Processing logicvar result = someOperation();// Must return with return statementreturn result;
Usage Examples
1. Basic Example - Direct Value Return
The simplest example directly returns a value:
return 1; // Returns number 1
2. Using Manual Input Parameters
// Configure input parameters:// Parameter name: input1// Parameter value: 123// Input method: Manual input
return input.input1; // Will return "123"
3. String Processing Example
Using context parameters for string concatenation:
// Configure input parameters:// Parameter name: name// Parameter value: $start.input1// Input method: From context
var output = "hello " + input.name; // Returns "hello [name]"return output;
4. Complex Logic Processing
// Example: Multiple conditional judgments and array processingvar data = input.data; // Get input datavar result = [];
// Array processingfor (var i = 0; i < data.length; i++) { if (data[i].value > 100) { result.push({ id: data[i].id, status: "high", // High value status }); } else if (data[i].value > 50) { result.push({ id: data[i].id, status: "medium", // Medium value status }); } else { result.push({ id: data[i].id, status: "low", // Low value status }); }}
return result; // Returns processed result array
5. Data Filtering & Transformation
// Example: Filter and transform product dataconst products = input.products;
return products .filter((p) => p.price > 100) // Filter high-priced items .map((p) => ({ id: p.id, name: p.name.toUpperCase(), // Standardize name price: p.price * 0.9, // Apply 10% discount }));
6. Type Conversion Handling
// Example: Handling number/string conversionsconst rawData = input.rawValue;
// Safe conversion logicreturn { stringValue: String(rawData).trim(), numberValue: isNaN(Number(rawData)) ? 0 : Number(rawData),};
7. Mathematical Operations Example
// Example: Statistical data analysisconst dataset = input.numbers;
return { average: dataset.reduce((a, b) => a + b) / dataset.length, maxValue: Math.max(...dataset), minValue: Math.min(...dataset),};
Best Practices
-
Code Organization
- Keep code concise and clear, avoid冗余。
- Add comments to explain the logic, helping others understand the code.
- Decompose complex logic into smaller functions to improve readability and maintainability.
-
Error Handling
try {// your business logicvar result = doSomething(); // execute some operationreturn result; // return result} catch (error) {return {error: true,message: error.message, // return error message};} -
Parameter Validation
if (!input.required_param) {return {error: true,message: "Missing required parameter", // missing required parameter};}
Limitations and Considerations
-
Execution Environment Restrictions
- Network requests are prohibited - ensure your code doesn’t rely on external data
- File system operations disabled - avoid file read/write logic
- 30-second execution timeout - optimize code efficiency
-
Code Security
- Avoid infinite loops to ensure proper termination
- Monitor memory usage to prevent leaks
- Never include sensitive data in code - protect user privacy
-
Debugging Recommendations
- Test complex logic locally before deployment
- Implement try-catch blocks to enhance error resilience
- Provide detailed error messages to facilitate rapid troubleshooting
-
Performance Monitoring
- Use
performance.now()
to measure execution time of critical code sections
const start = performance.now();// Critical logicconst duration = performance.now() - start;return { result: data, duration }; - Use