Set up and configure InfluxDB

In order to communicate with InfluxDB, the IoT Center application needs some basic information. The application must then be able to enforce the state of the InfluxDB configuration so that devices can reliably send API requests.

The InfluxDB specific parts of the IoT Center’s own API are in app/server/influxdb/. These contain functions for the onboarding process, authorizations, and organizations. We’ll place InfluxDB API functionality in this influxdb/ directory as we go.

The code we’ll look at below contains helper functions for configuration that will be used in the Express routes, which serve and handle the paths of the HTTP API.

As we build the server, we’ll link to locations in final code so you can see how it all comes together.

Create the project

To begin, create a new folder in your home directory:

mkdir $HOME/iot-center-app/

Within iot-center-app/, create a directory for our back-end code:

cd iot-center-app/
mkdir server

Configure InfluxDB with environment variables

We configure the application’s communication with InfluxDB by environment variables.

Open a text editor and save the following code as env.js in the iot-center-app/app/server/ directory. When the server starts, the following code will run to retrieve environment variables:

const INFLUX_URL = process.env.INFLUX_URL || 'http://localhost:8086'
const INFLUX_TOKEN = process.env.INFLUX_TOKEN || 'my-token'
const INFLUX_ORG = process.env.INFLUX_ORG || 'my-org'
const INFLUX_BUCKET = 'iot_center'

// Defaults when on boarding a fresh new InfluxDB instance
const onboarding_username = 'my-user' // InfluxDB user
const onboarding_password = 'my-password' // InfluxDB password

// recommended interval for clients to refresh configuration in seconds
const configuration_refresh = 3600

Let’s take these environment variables in turn:

  • The application server will us the INFLUX_TOKEN value to authenticate its requests to the InfluxDB API. This should be an all-access token. This is the server’s “master token” (so to speak) and should only be used by the IoT Center server.
  • INFLUX_URL tells the application where the InfluxDB server is located. By default, the server will look for a local installation of InfluxDB at http://localhost:8086.1
  • INFLUX_ORG is the name of the organization for the IoT Center to use.
  • INFLUX_BUCKET is the name of the bucket to which devices will write data.

With the configuration data now available, let’s add some basic logging to display them when the server starts:

// --snip--

function logEnvironment() {
  console.log(`INFLUX_URL=${INFLUX_URL}`)
  console.log(`INFLUX_TOKEN=${INFLUX_TOKEN ? '***' : ''}`)
  console.log(`INFLUX_ORG=${INFLUX_ORG}`)
  console.log(`INFLUX_BUCKET=${INFLUX_BUCKET}`)
}

Finally we need to export the variables so that they can be used elsewhere:

// --snip--

module.exports = {
  INFLUX_URL,
  INFLUX_TOKEN,
  INFLUX_ORG,
  onboarding_username,
  onboarding_password,
  configuration_refresh,
  INFLUX_BUCKET,
  logEnvironment,
}

Create server entrypoint file

From the server directory, let’s now create a file call index.js. This file will be responsible for starting the server.

We should now have a directory structure like this:

.
└── app
    └── server
        ├── env.js
        └── index.js

First, we will import INFLUX_URL from the environment variables we defined in env.js We will also import the Express web framework we'll be using to run the server. Paste the following into server/index.js:

const {logEnvironment, INFLUX_URL} = require('./env')
const express = require('express')
const proxy = require('express-http-proxy')
// --snip--

async function startApplication() {
  const app = express()

  // start proxy to InfluxDB to avoid CORS blocking with InfluXDB OSS v2 Beta
  app.use('/influx', proxy(INFLUX_URL))
  console.log(`Enable proxy from /influx/* to ${INFLUX_URL}/*`)

  // start HTTP server
  const port = process.env.PORT || 5000
  app.listen(port, process.env.HOSTNAME || '0.0.0.0')

  logEnvironment()
  console.log(`Listening on http://localhost:${port}`)
}
// --snip--

startApplication().catch((e) => {
  console.error('Failed to start', e)
})

Install Node dependencies and test run the server

Install the Node dependencies. From server/ directory, do:

yarn add express express-http-proxy

Let's test what we've written so far:

cd iot-center-app/app/server/
node index.js

You should see this output:

$ node index.js
Enable proxy from /influx/* to http://localhost:8086/*
INFLUX_URL=http://localhost:8086
INFLUX_TOKEN=***
INFLUX_ORG=my-org
INFLUX_BUCKET=iot_center
Listening on http://localhost:5000
1

Later, you might want to pass a URL for InfluxDB Cloud or another address on the local network.