Skip to content

ideas/hapi-service-discovery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hapi-service-discovery

npm Build Status Dependency Status

Service discovery plugin for the hapi framework.

Installation

npm install --save @ideapod/hapi-service-discovery

Configuration

The plugin accepts an options object where:

  • services: An array of service names.
const plugin = {
  register: HapiServiceDiscovery,
  options: {
    services: ['auth', 'user']
  }
};

The plugin iterates through the services array and retrieves the necessary information from environment variables. The environment variable format is SERVICE_<service name>_HOST and SERVICE_<service name>_PORT. For example: SERVICE_USER_HOST and SERVICE_USER_PORT.

Usage

Registration

The plugin can be registered as a hapi plugin. Example:

const Hapi = require('hapi');
const HapiServiceDiscovery = require('@ideapod/hapi-service-discovery');

const server = new Hapi.Server();
server.connection();

const plugin = {
  register: HapiServiceDiscovery,
  options: {
    services: ['auth', 'user']
  }
};

server.register(plugin, (err) => {
  // ...
});

Exposed properties

Services are exposed under the services property.

const UserService = server.plugins['hapi-service-discovery'].services.user;

Each exposed service has a request() method which makes a request to a configured service. The signature of the function is request(path, options) where:

  • path: The requested path (must begin with '/').
  • options: Optional request options.
const UserService = server.plugins['hapi-service-discovery'].services.user;

UserService.request('/test', { method: 'POST', body: { test: 'test' }, json: true })
  .then((response) => {
    // ...
  });

Under the hood the plugin uses the request module. Options used in request() are directly passed to the request module.

About

Service discovery plugin for the hapi framework.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors