edit-icon download-icon

ACM Node.js SDK

Last Updated: Jan 17, 2018

Install the ACM Client for Node.js

ACM Client for Node.js makes it possible to help front-end developers release the front and back ends independently, and separate development from operations, which improves the development efficiency dramatically.

Enter the following command to install the client:

  1. npm i acm-client --save

Sample code

Run the following code in your project for configuration management.

  1. const ACMClient = require('acm-client');
  2. const co = require('co');
  3. const acm = new ACMClient({
  4. endpoint: 'acm.aliyun.com', // Can be obtained from the ACM console.
  5. namespace: '***************', // Can be obtained from the ACM console.
  6. accessKey: '***************', // Can be obtained from the ACM console.
  7. secretKey: '***************', // Can be obtained from the ACM console.
  8. requestTimeout: 6000, // Length of request time-out, 6s by default.
  9. });
  10. // Actively pull the configuration.
  11. co(function*() {
  12. const content= yield acm.getConfig('test', 'DEFAULT_GROUP');
  13. console.log('getConfig = ',content);
  14. });
  15. // Listen for data updating.
  16. acm.subscribe({
  17. dataId: 'test',
  18. group: 'DEFAULT_GROUP',
  19. }, content => {
  20. console.log(content);
  21. });

Error events handling

  1. acm.on('error', function (err) {
  2. // Logs can be recorded here in a unified manner.
  3. // If error events are not listened for, all exceptions get printed to stderr.
  4. });

API description

Configuration getting API

Used to get the configuration from ACM when the service starts.

  1. function* getConfig(dataId, group)

Request parameters

Parameter name Parameter type Description
dataId String Configuration ID. Use a naming rule like package.class (for example, com.taobao.tc.refund.log.level) to ensure global uniqueness.
It is recommended to indicate business meaning of the configuration in the “class” section.
Use lower case for all characters. Use alphabetical letters and these four special characters (“.”, “:”, “-“, “_”) only. Up to 256 characters are allowed.
group String Configuration group. To ensure uniqueness, format such as product name: module name (for example, ACM:Test) is preferred.
Use alphabetical letters and these four special characters (“.”, “:”, “-“, “_”) only. Up to 128 characters are allowed.

Return values

Parameter type Description
String Configuration value

Configuration listening API

Use the ACM dynamic configuration listening API to enable ACM to push configuration changes.

  1. function subscribe(info, listener)

Request parameters

Parameter name Parameter type Description
info Object Info.dataId: Configuration ID.
info.group: Configuration group
listener Function Listener. Configuration changes go into the callback function of the listener.

Cancel configuration listening API

Used to cancel configuration listening when the service starts.

  1. function unSubscribe(info, [listener])

Request parameters

Parameter name Parameter type Description
info Object info.dataId: Configuration ID.
Info.group: Configuration group
listener Function Callback function. (Optional, all listener functions are removed if this parameter is not passed in.)

Node.js project link

https://www.npmjs.com/package/acm-client

Feedback

Thank you! We've received your feedback.