https://github.com/mdb/node-consul-kv
A tiny NPM package for interacting with Consul KV store
https://github.com/mdb/node-consul-kv
consul consul-kv node
Last synced: 9 months ago
JSON representation
A tiny NPM package for interacting with Consul KV store
- Host: GitHub
- URL: https://github.com/mdb/node-consul-kv
- Owner: mdb
- License: mit
- Created: 2017-05-20T01:15:34.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2023-01-12T14:35:41.000Z (over 3 years ago)
- Last Synced: 2024-11-14T20:46:54.469Z (over 1 year ago)
- Topics: consul, consul-kv, node
- Language: JavaScript
- Size: 287 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/mdb/node-consul-kv/actions/workflows/cicd.yaml)
# consul-kv
A tiny NPM package providing a minimal [Consul KV store](https://www.consul.io/api/kv.html) client.
## Using consul-kv
### Instantiating:
```javascript
const Consul = require('consul-kv');
const consul = new Consul({
host: 'my-consul.com', // required
token: 'my-acl-token', // optional
tlsCert: '', // optional
tlsKey: '', // optional
ca: '', // optional
port: '8500', // optional; defaults to '8500'
protocol: 'https', // optional; defaults to 'https'
});
```
### Usage
Create or update a key:
```javascript
const resp = await consul.set('my/key', 'my-key-value');
```
Read a key:
```javascript
const result = await consul.get('my/key');
console.log(result.value); // the key's value; undefined if it doesn't exist
console.log(result.responseStatus); // the HTTP status code of the Consul response
console.log(result.responseBody); // the HTTP body of the Consul response
```
Read a the full subtree below a key (this adds a `?recurse` query to the request, per [Consul documentation](https://www.consul.io/api/kv.html)):
```javascript
const result = await consul.get('my/key', { recurse: true });
console.log(result); // the entire 'my/key' subtree
console.log(result.responseStatus); // the HTTP status code of the Consul response
console.log(result.responseBody); // the HTTP body of the Consul response
```
Delete a key:
```javascript
const resp = await consul.delete('my/key');
```
Bonus: issue your own requests & get the raw response:
```javascript
const resp = await consul.request({
key: 'my/key',
body: 'my-value-or-optional-request-body',
method: 'put'
});
```
## Development
Install dependencies & run unit tests:
```
npm install
npm test
```
Run end-to-end tests against a local Consul using [docker-compose](https://docs.docker.com/compose/):
```
docker-compose up --detach
npm run test:e2e
```