

Overview
This template generates a Node.js application with any of the supported protocols endpoint, based on Hermesjs.
Other files are for the setup of developer environment, like .editorconfig or .eslint.
Technical requirements
Specification requirements
operationId | Operation ID must be set for every operation to generate proper functions as there is no fallback in place | - | - |
Supported protocols
How to use the template
This template must be used with the AsyncAPI Generator. You can find all available options here.
In case you use X509 security and need to provide certificates, either place them in the root of the generated server with the following names: ca.pem, service.cert, service.key. You can provide a custom directory where cert files are located using certFilesDir parameter like -p certFilesDir=../not/in/my/app/dir.
Since you can have multiple different security schemes, to use the one of X509 type, you need to pass the name of the scheme like this: -p securityScheme=SCHEME_NAME.
You can find a complete tutorial on AsyncAPI Generator using this template here.
CLI
$ npm install -g @asyncapi/cli
$ asyncapi generate fromTemplate test/mocks/mqtt/asyncapi-v3.yml @asyncapi/nodejs-template -o test/output -p server=production
$ asyncapi generate fromTemplate test/mocks/mqtt/asyncapi-v3.yml ./ -o test/output -p server=production
$ cd test/output
$ npm i
$ cd ../..
$ npm run test:example
$ npm install mqtt -g
$ mqtt pub -t 'smartylighting/streetlights/1/0/event/123/lighting/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": 3, "sentAt": "2017-06-07T12:34:32.000Z"}'
$ mqtt sub -t 'smartylighting/streetlights/1/0/action/1/turn/on' -h 'test.mosquitto.org'
Adding custom code / handlers
It's highly recommended to treat the generated template as a library or API for initializing the server and integrating user-written handlers. Instead of directly modifying the template, leveraging it in this manner ensures that its regenerative capability is preserved. Any modifications made directly to the template would be overwritten upon regeneration.
Consider a scenario where you intend to introduce a new channel or section to the AsyncAPI file, followed by a template regeneration. In this case, any modifications applied within the generated code would be overwritten.
To avoid this, user code remains external to the generated code, functioning as an independent entity that consumes the generated code as a library. By adopting this approach, the user code remains unaffected during template regenerations.
Facilitating this separation involves creating handlers and associating them with their respective routes. These handlers can then be seamlessly integrated into the template's workflow by importing the appropriate methods to register the handlers. In doing so, the template's client.register<operationId>Middleware method becomes the bridge between the user-written handlers and the generated code. This can be used to register middlewares for specific methods on specific channels.
Look closely into example script that works with library generated using this MQTT based AsyncAPI document. Look at available handlers API for reading incomming messages and processing outgoing messages. Learn how to start generated server with init() and also learn how to send messages, if needed.
Template configuration
You can configure this template by passing different parameters in the Generator CLI: -p PARAM1_NAME=PARAM1_VALUE -p PARAM2_NAME=PARAM2_VALUE
| server | The server you want to use in the code. | Yes | production |
| securityScheme | Name of the security scheme. Only scheme with X509 and Kafka protocol is supported for now. | No | 'mySchemeName' |
| certFilesDir | Directory where application certificates are located. This parameter is needed when you use X509 security scheme and your cert files are not located in the root of your application. | No | ../not/in/my/app/dir |
Development
The most straightforward command to use this template is:
$ ag https://bit.ly/asyncapi @asyncapi/nodejs-template -o output -p server=production
Setup locally
$ git clone https://github.com/{username}/nodejs-template
$ cd nodejs-template
$ npm install
$ ag https://bit.ly/asyncapi ./ -o output -p server=production
For local development, you need different variations of this command. First of all, you need to know about three important CLI flags:
--debug enables the debug mode in React rendering engine what makes filters debugging simpler.
--watch-template enables a watcher of changes that you make in the template. It regenerates your template whenever it detects a change.
--install enforces reinstallation of the template.
There are two ways you can work on template development:
Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!