# consulite

> Tiny consul Node.js module for client discovery

Latest version **3.0.0** (published 2020-08-31) · MPL-2.0 license · 0 weekly downloads

## Install

```sh
npm install consulite
pnpm add consulite
yarn add consulite
bun add consulite
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2020-08-31 |
| First published | 2016-08-24 |
| Weekly downloads | 0 |
| License | MPL-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 24.3 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 29 |
| Author | Wyatt Preul |
| Maintainers | wyatt |

## Links

- npm: https://www.npmjs.com/package/consulite
- Repository: https://github.com/joyent/node-consulite
- Homepage: https://github.com/joyent/node-consulite#readme
- Issues: https://github.com/joyent/node-consulite/issues
- npm.io page: https://npm.io/package/consulite

## Dependencies (2)

- [or-promise](https://npm.io/package/or-promise.md) 1.x.x
- [@hapi/wreck](https://npm.io/package/@hapi/wreck.md) 17.x.x

## Recent versions

- 3.0.0 (latest) — 2020-08-31
- 2.0.0 — 2017-06-29
- 1.8.0 — 2017-06-29
- 1.7.0 — 2017-06-26
- 1.6.0 — 2017-03-09
- 1.5.2 — 2016-11-29
- 1.5.1 — 2016-09-09
- 1.5.0 — 2016-09-07
- 1.4.0 — 2016-08-29
- 1.3.0 — 2016-08-29
- 1.2.1 — 2016-08-24
- 1.2.0 — 2016-08-24
- 1.1.0 — 2016-08-24
- 1.0.0 — 2016-08-24

## README

# consulite
Tiny consul Node.js module for client discovery with round-robin support

[![Npm Version](https://img.shields.io/npm/v/consulite.svg)](https://npmjs.com/package/consulite)
[![Node Version](https://img.shields.io/node/v/consulite.svg)](https://npmjs.com/package/consulite)
[![Build Status](https://secure.travis-ci.org/joyent/node-consulite.svg)](http://travis-ci.org/joyent/node-consulite)

## API


### new Consulite(options)

Create a new instance of the Consulite class. `options` can include the following properties:

* `consul` - consul host to connect to. Defaults to either:
  * `${process.env.CONSUL_HOST}:${process.env.CONSUL_PORT}`
  * `consul:8500` - as a last resort


### getServiceNames([, callback])

Get all service names from consul, regardless of health status.

* `callback`: function with the signature `(err, services)` where `services` is an array of service names.

This function returns a `Promise` if no `callback` is provided.


### getServiceStatus(name, [, callback])

Get all nodes for the service and include their health status.

* `callback`: function with the signature `(err, nodes)` where `nodes` is an array of node data, which is formatted with the properties shown in the example below.

```
[
  {
    node: 'foobar',
    address: '10.1.10.12',
    port: 8000,
    status: 'passing'
  }
]
```

This function returns a `Promise` if no `callback` is provided.


### getService(name [, callback])

Get service address information from cache or from consul. When multiple service
instances are registered with consul the first instance that hasn't been executed
or the oldest executed service is returned.

* `name`: the service name registered with consul. If no services are found
then an error will be returned to the callback. If multiple services are found
then the service that hasn't been executed or hasn't been executed most recently
will be returned in the callback.

* `callback`: function with the signature `(err, service)` where `service` has
the following properties:
  - `address`: the host address where the service is located
  - `port`: the port that the service is exposed on

This function returns a `Promise` if no `callback` is provided.


### getServiceHosts(name [, callback])

Get an array of all service instances from cache or from consul.

* `name`: the service name registered with consul. If no services are found
then an error will be returned to the callback.

* `callback`: function with the signature `(err, hosts)` where `hosts` is an
array of objects with the following properties:
  - `address`: the host address where the service is located
  - `port`: the port that the service is exposed on

This function returns a `Promise` if no `callback` is provided.


### getCachedService(name)

Get the next service from the cache if it exists, otherwise return null;


### getCachedServiceHosts(name)

Get an array of all service instances from the cache if it exists, otherwise
null.


### refreshService(name [, callback])

Makes a request to consul for the given service name and caches the results. Only
services that are healthy are cached.

* `name`: the service name to fetch from consul.
* `callback`: function with signature `(err, services)`

This function returns a `Promise` if no `callback` is provided.


## Example Usage

```js
const Consulite = require('consulite');
const Wreck = require('wreck');

const consulite = new Consulite();
consulite.getService('users', (err, service) {
  if (err) {
    console.error(err);
    return;
  }

  Wreck.get(`http://${service.address}:${service.port}/users`, (err, res, payload) => {
    // handle error and do something with results
  });
});
```

If you want to set the consul address to use and don't want to depend on
environment variables you can use the `config()` function as demonstrated below:

```js
const Consulite = require('consulite');
const Wreck = require('wreck');

const consulite = new Consulite({ consul: 'http://myconsul.com' });

consulite.getService('users', (err, service) {
  if (err) {
    console.error(err);
    return;
  }

  Wreck.get(`http://${service.address}:${service.port}/users`, (err, res, payload) => {
    // handle error and do something with results
  });
});
```

---
_Source: https://npm.io/package/consulite · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
