NodeJS Component
Use the NodeJS component to execute custom JavaScript and NodeJS code, complete with NPM package support, directly within your workflows. This component runs your code in a secure, serverless environment, giving you an infinitely flexible tool for any logic that goes beyond the standard components.
What You’ll Configure
- Write Your NodeJS Code
- Configure Inputs
- Handle Outputs
- Configure Advanced Settings (Custom AWS)
- Best Practices
- Troubleshooting Tips
- What to Try Next
Step 1: Write Your NodeJS Code
The component requires your code to be structured within an async function main
. The inputs you define in the UI are automatically passed as arguments to this function.
Code Structure
// You can import public NPM packages
import axios from 'axios';
// Inputs from the UI are passed as arguments here, in order.
async function main(input1, input2) {
// Your code logic goes here
// For example, make an API call using an imported package
try {
const response = await axios.get(`https://api.example.com/data?param=${input1}`);
const processedData = {
...response.data,
customField: input2
};
// The value you return will be available in the "Output" port
return processedData;
} catch (error) {
// Errors can be handled and will be available in the "Error" port
console.error('An error occurred:', error.message);
throw error; // Re-throwing the error makes it available in the Error output
}
}
Step 2: Configure Inputs
For every piece of data your code needs, you will add a corresponding input. These inputs are passed as parameters to your main
function in the order they are created.
Action | Description |
---|---|
Add Input | Click the + icon to add a new input. Give it a descriptive name (e.g., user_id , api_data ). The value passed to user_id will be the first argument to main , the next input will be the second, and so on. |
Step 3: Handle Outputs
The component provides default Output
and Error
ports, and allows for custom outputs to easily parse a JSON response.
Output | Description |
---|---|
Output | This port contains the value returned by your main function. If you return a JSON object, you can parse it using custom outputs. |
Error | If your code throws an error, this port will contain the error object, which is useful for debugging and error handling in your workflow. |
Custom Outputs | If your function returns a JSON object (e.g., { "status": "success", "data": {...} } ), you can add custom outputs named status and data to extract those fields directly without needing a separate parsing step. |
Step 4: Configure Advanced Settings (Custom AWS)
For greater control, you can configure the component to run the code in your own AWS account instead of the managed SmythOS infrastructure.
Setting | Description |
---|---|
Use Custom Cloud Keys | Toggle this on to use your own AWS Lambda environment. |
AWS Access Key ID | Your AWS Access Key ID. Required when using your own AWS account. |
AWS Secret Key | Your AWS Secret Key. Required when using your own AWS account. |
AWS Region | The AWS region where the function should be deployed (e.g., us-east-1 ). |
Best Practices
- Return Structured JSON: Instead of returning a simple string or number, always have your function
return
a structured JSON object. This makes it much easier to use the data in downstream components via custom outputs. - Handle Errors Gracefully: Use
try...catch
blocks within your code to handle potential errors (like failed API calls). You can log the error andthrow
it to make it available in theError
output port for proper workflow error handling. - Keep Code Focused: This component is best for single, specific tasks. For very complex, multi-step logic, consider breaking it down into several
NodeJS
components or chaining them with other SmythOS components. - Manage Dependencies: While any public NPM package can be used, be mindful of their size and cold start times, as very large dependencies can increase execution latency.
Troubleshooting Tips
What to Try Next
- Use this component to connect to a service that doesn't have a dedicated integration by making requests with
axios
ornode-fetch
. - Connect this to the output of an API Call Component to perform a custom transformation on the response data that is too complex for a simple filter.