Skip to content

Distribute FeathersJS apps over the network with inter-service communication using HTTP protocol

License

Notifications You must be signed in to change notification settings

dekelev/feathers-http-distributed

Repository files navigation

feathers-http-distributed

Build Status Coverage Status js-semistandard-style Dependency Status npm

Distribute FeathersJS apps over the network with inter-service communication using HTTP protocol.

Install

npm install --save feathers-http-distributed

Init

const feathers = require('@feathersjs/feathers');
const distributed = require('feathers-http-distributed');

const app = feathers();

app.configure(distributed({}));
Options
Option Type Default Required Description
protocol string 'http' no Protocol to use when calling remote services.

Supported protocols are http & https.
host string null no Default hostname to use when calling remote services.
port number 80 no Port number to use when calling remote services.

Defaults to the default port of the selected protocol.
dnsSuffix string '' no DNS suffix that will be added to the host when calling remote services.
pathToHost boolean
function
false no If host is not set, path will be converted into host by replacing all the non-alphanumeric characters to -.

Can also be set with a custom method that receives a path and returns a host.
timeout number 0 no Request timeout in milliseconds.

Set to 0 to disable timeout.

If timeout is enabled, by default, it will include the time spent on retries.
proxy object null no Transparent HTTP proxy to forward requests to remote services.

Set the proxy object with host & port.

If proxy authentication is required, set the auth key with object containing the username & password keys.
excludeParams string[] null no List of keys to exclude from the params object of the remote service call before sending the request.
maxRedirects number 5 no Maximum redirects to follow.

Set to 0 to disable redirects.
keepAlive boolean false no Use HTTP persistent connections with HTTP keep-alive.
internalRequestHeader string X-Internal-Request no Name of the request header that is sent with each request to remote service.

This header is used to identify the request as internal and contains the params object of the service call.

Add rule in your external API Gateway or load-balancer to remove this header from all incoming requests.
retry boolean
object
false no Retry failed requests on a network error or when receiving 5xx error on an idempotent request (GET, HEAD, OPTIONS, PUT or DELETE).

By default, it will retry failed requests 3 times without delay.

List of all the supported retry options is available here.

Call remote service

const result = await app.service('remote').find({});

const result = await app.service('remote').find({ host: 'remote-app' });
Params
Option Type Required Description
protocol string no Overrides the protocol init option.
host string no Overrides the host and pathToHost init options.
port number no Overrides the port init option.
dnsSuffix string no Overrides the dnsSuffix init option.
timeout number no Overrides the timeout init option.
proxy object no Overrides the proxy init option.

Middleware

Use the handleInternalRequest method to detect and handle incoming HTTP requests from remote FeathersJS apps.

When handleInternalRequest returns true, skip any further custom middlewares that should only apply to external HTTP requests.

const { handleInternalRequest } = require('feathers-http-distributed');

app.use((req, res, next) => {
  if (handleInternalRequest(req)) {
    next();

    return;
  }

  // Add here custom middlewares that only applies to external HTTP requests
});

Security

Secure your network by adding a rule in your external API Gateway or load-balancer to remove the X-Internal-Request request header from all the incoming requests.

Debug logs

Debug logs can be enabled by settings the DEBUG environment variable to feathers-http-distributed*,axios.

Debugging in Kubernetes

The proxy option can be used to forward requests from a FeathersJS app running locally to remote services inside Kubernetes clusters with the help of transparent HTTP proxies.

Tools like Telepresence helps with debugging incoming traffic that goes into Kubernetes pods, by swapping the pods with proxy pods and redirects incoming traffic to a local port on the host.

With the proxy option set, you can simply run Telepresence with the inject-tcp proxying method and debug your FeathersJS app as you normally do.

See here for example of deploying transparent HTTP proxy with Docker.