atlassian-connect-express 5: add parameters to config.json

Hello!

This article is based on the previous article.

You can find the source code for the article here.

You can watch the video for this article here.

In this article we will discuss how to add your own parameters to config.json file.

As you remember backend/config.json contains parameters which are passed to the application when the application is starting. These parameters are passed by developers or administrators, that is why these parameters are not hardcoded somewhere in the code, but are placed in the config.json file which is easy to edit.

You may need your own parameters if you introduce logic which depends on some switchers. In this case you can add the switchers to the config.json file. Or if you call some kind of Rest Api and the url of the Rest Api is different for development and production. In this case you can add the url of the Rest Api to the config.json. Or if you are connecting to some external application and you need to provide the user name and the token to connect to this system.

We will be changing the development section of config.json. The same can to be done to the production section. Here is our development section:

    "development": {
        "port": 3000,
        "errorTemplate": true,
        "store": {
            "adapter": "sequelize",
            "dialect": "sqlite3",
            "type": "memory"
        },
        "localBaseUrl": "https://a8345332fbcc.ngrok.io"
    },

First, let’s make our app read the port and localBaseUrl parameters from environment variables:

"development": {
        "port": "$PORT",
        "errorTemplate": true,
        "store": {
            "adapter": "sequelize",
            "dialect": "sqlite3",
            "type": "memory"
        },
        "localBaseUrl": "$APP_URL"
    },

As you can see the port value will be taken from the PORT environment variable and the local base url from APP_URL environment variable.

Now let’s add new parameters to our config file. We will add a REST API url and credentials for our external app:

"development": {
        "port": "$PORT",
        "errorTemplate": true,
        "store": {
            "adapter": "sequelize",
            "dialect": "sqlite3",
            "type": "memory"
        },
        "localBaseUrl": "$APP_URL",
        "restApiUrl": "https://my.domain.com/rest/2/",
        "externalApp" : {
            "username" : "myusername",
            "token": "mytoken"
        }   
    },

I created the restApiUrl parameter which has just a string value and the externalApp parameter the value of which is a json. I could have also created these parameters as variables like the port and externalApp parameters, but for the sake of simplicity I will not do it.

Now let’s add some code to our application to read all these parameters:

http.createServer(app).listen(port, () => {
  console.log('App server running at ' + addon.config.localBaseUrl());
  console.log(`port: ${addon.config.port()}`)
  console.log(`localBaseUrl: ${addon.config.localBaseUrl()}`)
  console.log(`restApiUrl: ${addon.config.restApiUrl()}`)
  console.log(`externalApp: ${JSON.stringify(addon.config.externalApp())}`)
});

As you can see to read parameters you need just to call addon.confg.<the name of the parameter>().

The externalApp parameter we defined as json, that is why an object in json format is returned. And that is why we stringify the output to see the output in the terminal: JSON.stringify(addon.config.externalApp())

Let ‘s run our application. As a result we will have an error:

Error: listen EADDRINUSE: address already in use $PORT

It means that we did not initialize our environment variable PORT. Let’s initialize both the PORT and APP_URL variables:

export PORT=3000
export APP_URL=https://732d2b930817.ngrok.io

Now let’s run our application again and here is the output:

pport: 3000
localBaseUrl: https://732d2b930817.ngrok.io
restApiUrl: https://my.domain.com/rest/2/
externalApp: {"username":"myusername","token":"mytoken"}

That is it. Now we can define our own parameters and read the parameters in our app.

Thank you for reading!

1 comment

Comment

Log in or Sign up to comment
M Amine
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 4, 2021

Interesting content, thank you @Alexey Matveev 

TAGS
AUG Leaders

Atlassian Community Events