1.1.0 • Published 1 year ago

feathers-http-distributed v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
1 year ago

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
OptionTypeDefaultRequiredDescription
protocolstring'http'noProtocol to use when calling remote services.Supported protocols are http & https.
hoststringnullnoDefault hostname to use when calling remote services.
portnumber80noPort number to use when calling remote services.Defaults to the default port of the selected protocol.
dnsSuffixstring''noDNS suffix that will be added to the host when calling remote services.
pathToHostbooleanfunctionfalsenoIf 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.
timeoutnumber0noRequest timeout in milliseconds.Set to 0 to disable timeout.If timeout is enabled, by default, it will include the time spent on retries.
proxyobjectnullnoTransparent 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.
excludeParamsstring[]nullnoList of keys to exclude from the params object of the remote service call before sending the request.
maxRedirectsnumber5noMaximum redirects to follow.Set to 0 to disable redirects.
keepAlivebooleanfalsenoUse HTTP persistent connections with HTTP keep-alive.
internalRequestHeaderstringX-Internal-RequestnoName 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.
retrybooleanobjectfalsenoRetry 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
OptionTypeRequiredDescription
protocolstringnoOverrides the protocol init option.
hoststringnoOverrides the host and pathToHost init options.
portnumbernoOverrides the port init option.
dnsSuffixstringnoOverrides the dnsSuffix init option.
timeoutnumbernoOverrides the timeout init option.
proxyobjectnoOverrides 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.