Node.js Spring Cloud Config Client

1.6.2 · active · verified Wed Apr 22

The `cloud-config-client` library provides a Node.js implementation for connecting to and consuming configuration from a Spring Cloud Config Server. Currently at version 1.6.2, it enables Node.js applications to fetch dynamic configurations, supporting application names, profiles, and labels as defined by the Spring Cloud specification. It handles property resolution based on Spring's hierarchy rules and offers a simple API for retrieving values by key, including context-based substitutions. The library provides both Promise-based and Node.js-style callback interfaces for configuration loading and ships with TypeScript types for enhanced developer experience. It differentiates itself by offering a direct, opinionated integration with the Spring Cloud Config ecosystem within Node.js environments, rather than a generic key-value store client.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates fetching configuration asynchronously, including basic authentication, profile and label usage, context-based substitutions, and handling of self-signed certificates.

import client, { Config } from 'cloud-config-client';
import * as https from 'https';

async function fetchConfig() {
  try {
    // Ensure CONFIG_SERVER_URL, CONFIG_USERNAME, CONFIG_PASSWORD are set in environment
    const config: Config = await client.load({
      endpoint: process.env.CONFIG_SERVER_URL ?? 'http://localhost:8888',
      name: 'my-application', // 'application' is deprecated, use 'name'
      profiles: ['development', 'aws'],
      label: 'main',
      auth: {
        user: process.env.CONFIG_USERNAME ?? 'configuser',
        pass: process.env.CONFIG_PASSWORD ?? 'configpassword'
      },
      // Reject unauthorized (self-signed) certificates in production, allow in dev
      rejectUnauthorized: process.env.NODE_ENV === 'production',
      // Provide environment variables as context for placeholder substitution
      context: {
        ...process.env,
        APP_VERSION: '1.0.0' // Custom context value
      },
      headers: {
        'X-Trace-ID': 'my-app-trace-123'
      },
      agent: process.env.HTTPS_PROXY ? new https.Agent({ proxy: process.env.HTTPS_PROXY }) : undefined
    });

    console.log('Configuration loaded successfully!');

    const databaseUrl = config.get('database.url');
    console.log(`Database URL: ${databaseUrl}`);

    const welcomeMessage = config.get('app.message', 'key01'); // Example with prefix
    console.log(`Welcome Message: ${welcomeMessage}`);

    // Iterate over all properties (excluding overridden by default)
    console.log('\nAll properties:');
    config.forEach((key, value) => {
      console.log(`  ${key}: ${value}`);
    });

    // Access raw properties if needed
    // console.log('\nRaw config properties:', config.raw);

  } catch (error: any) {
    console.error('Failed to load configuration:', error.message);
    if (error.response) {
      console.error('Server response status:', error.response.status);
    }
  }
}

fetchConfig();

view raw JSON →