Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

AsyncAPI logo
AsyncAPI Node.js Template

npm npm

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

Property name Reason Fallback Default
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

# Install the AsyncAPI Generator
npm install -g @asyncapi/generator

# Run generation
ag https://bit.ly/asyncapi @asyncapi/nodejs-template -o output -p server=production

##
## Start the server 
##

# Go to the generated server
cd output

# Build generated application
npm i

# Start server
# To enable production settings start the server with "NODE_ENV=production npm start"
npm start

##
## Start the client 
##

#for testing your server you can use mqtt client. open a new terminal and install it using:
npm install mqtt -g

#publish an invalid message.
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"}'

#publish a valid message
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"}'

#You should see the sent message in the logs of the previously started server.
#Notice that the server automatically validates incoming messages and logs out validation errors

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

Name Description Required Example
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

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 Nunjucks 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:

  • Use global Generator and template from your local sources:
    # assumption is that you run this command from the root of your template
    ag https://bit.ly/asyncapi ./ -o output -p server=production
  • Use Generator from sources and template also from local sources. This approach enables more debugging options with awesome console.log in the Generator sources or even the Parser located in node_modules of the Generator:
    # assumption is that you run this command from the root of your template
    # assumption is that generator sources are cloned on the same level as the template
    ../generator/cli.js https://bit.ly/asyncapi ./ -o output -p server=production

Contributors

Thanks goes to these wonderful people (emoji key):


Fran Méndez

💻 🤔

Lukasz Gornicki

🚇 💻

Khuda Dad Nomani

📖 💻

This project follows the all-contributors specification. Contributions of any kind welcome!