1.0.0 • Published 8 months ago

qflow-node-generator-template v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

Node.js Application Template

This is a template for creating Node.js applications with specific functionalities, including dispatch handling, Redis integration, variable parsing, and callback mechanisms.

Application Logic

  1. The application sets up an Express server with a route for /dispatch that accepts query parameters: qfiid, responseMode, and nodeName.
  2. The body of the dispatch call should contain nodeParameters and systemParameters objects.
  3. The application parses nodeParameters, replacing variables in double curly braces (e.g., {{start.callFrom}}) with their corresponding values from Redis.
  4. Based on the responseMode, the application either:
    • Executes the logic immediately and returns a callback data object (for instant mode)
    • Returns an immediate response, then executes the logic and makes a separate callback request (for callback mode)
  5. The application includes utilities for Redis operations, making HTTP requests for callbacks, and parsing node parameters.
  6. There's a placeholder for the main logic that users can customize when creating their own applications from this template.

Key Features

  • Dynamic Redis Integration: Connects to Redis using a connection string provided in the dispatch call.
  • Variable Parsing: Automatically replaces variables in nodeParameters with values from Redis.
  • Flexible Parameter Handling: Supports complex nodeParameters structures, including nested objects and arrays.
  • Dual Response Modes: Supports both instant and callback response modes.
  • Customizable Logic: Easily extendable main logic for specific application needs.

How to Run

  1. Clone this repository
  2. Install dependencies: npm install
  3. Ensure Redis is running and accessible
  4. Start the application: npm start

The server will start on http://localhost:8500 (or the port specified in the PORT environment variable).

Using as a Template

To use this as a template for creating new applications:

  1. Ensure you have Node.js and npm installed.
  2. Run the following command:
    npx node-template-app
  3. Follow the prompts to enter your node name.
  4. A new application will be generated based on this template.

Placeholders and Customization

The main logic of your application should be implemented in src/mainLogic.js. This file contains a placeholder function that you should replace with your own logic.

Other areas that may need customization:

  • src/index.js: Update port or add additional routes if needed.
  • src/dispatchHandler.js: Modify the callback data structure if required.
  • src/redisUtil.js: Adjust Redis operations as needed for your application.
  • src/httpUtil.js: Modify the callback URL or add additional HTTP utilities if required.
  • src/parameterParser.js: Adjust the variable parsing logic if needed.

Redis Key Structure

Redis keys are structured as follows: ${qfiid}.${nodeName}.${variable name}

For example, a variable {{start.callFrom}} for QFIID "123" would be stored in Redis as 123.start.callFrom.

Additional endpoints

  • /router-log: an endpoint to send create a new router log entry. the function can be found in httpUtil file it uses routerURL, routerLogEndpoint which can be found in the dispatchRequest.body.systemParameters variable it required the following data as request body { qfiid, action, applicationId, nodeName, payload, httpResponseCode }

  • /customer-log: an endpoint to send create a new customer application log entry. the function can be found in httpUtil file it uses routerURL, customerApplicationLogEndpoint which can be found in the dispatchRequest.body.systemParameters variable it required the following data as request body { applicationId, customerId, qfiid, nodeName, nodeType, nodeTitle, event }

Testing

To test the application:

  1. Set test values in Redis:

    redis-cli
    SET 123.start.callFrom "testCallFrom"
    SET 123.play_1.url "http://example.com/play"
  2. Send a test request:

    curl "http://localhost:9000/dispatch?qfiid=123&responseMode=instant&nodeName=testNode" \
    -H "Content-Type: application/json" \
    -d '{
      "nodeParameters": {
        "eventUrl": "{{play_1.url}}",
        "apiRequestPayload": [
          {
            "key": "{{start.callFrom}}",
            "value": ""
          }
        ]
      },
      "systemParameters": {
        "redisConnectionString": "redis://localhost:6379",
        "routerURL": "http://localhost:8080"
      }
    }'
  3. Check the console output for logs showing the original and parsed nodeParameters.

File Structure

Please refer to the file_structure.txt file for a detailed overview of the project's file organization.

Publishing the Package

If you want to make your template available via npx, you can publish it to npm:

  1. Make sure you have an npm account and are logged in (npm login).
  2. Update the package.json file with your package name, version, and other details.
  3. Run npm publish to publish your package.

Once published, others can use your template by running npx your-package-name.

1.0.0

8 months ago