qflow-node-generator-template v1.0.0
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
- The application sets up an Express server with a route for
/dispatch
that accepts query parameters:qfiid
,responseMode
, andnodeName
. - The body of the dispatch call should contain
nodeParameters
andsystemParameters
objects. - The application parses
nodeParameters
, replacing variables in double curly braces (e.g.,{{start.callFrom}}
) with their corresponding values from Redis. - 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)
- The application includes utilities for Redis operations, making HTTP requests for callbacks, and parsing node parameters.
- 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
- Clone this repository
- Install dependencies:
npm install
- Ensure Redis is running and accessible
- 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:
- Ensure you have Node.js and npm installed.
- Run the following command:
npx node-template-app
- Follow the prompts to enter your node name.
- 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:
Set test values in Redis:
redis-cli SET 123.start.callFrom "testCallFrom" SET 123.play_1.url "http://example.com/play"
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" } }'
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:
- Make sure you have an npm account and are logged in (
npm login
). - Update the
package.json
file with your package name, version, and other details. - Run
npm publish
to publish your package.
Once published, others can use your template by running npx your-package-name
.
8 months ago