Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Mohammed Amine Community Leader Jun 04, 2021

Interesting content, thank you @Alexey Matveev _Appfire_ 

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Jira issue check and more advanced commit verifications for Bitbucket DC

Pre-receive hooks that verify the Git commit message, the modified files, and implement similar code change controls used to be requirements of large enterprises working in regulated industries only....

24 views 0 0
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you