npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fh-mbaas-proxy

v1.0.0

Published

Creates an express middleware to enable proxying of Cloud calls to an MBaaS Service

Downloads

12

Readme

fh-mbaas-proxy

Circle CI

MBaaS Services are one of the most useful features of the Red Hat Mobile Application Platform. These services enable developers to expose reusable RESTful APIs (or anything else that you can serve over HTTP!) that can be used by multiple projects. One drawback of this however is that code duplication can occur in the dependent cloud applications since you might find the need to call the same endpoints in each using $fh.service, and return those results to a Client Application.

fh-mbaas-proxy aims to resolve this duplication by serving as an express middleware that can be added to any Cloud Application to expose service routes as if they were part of it. Less time writing calls to $fh.service means more time getting features done, and less bugs!

Install

npm install fh-mbaas-proxy --save

Usage

Simple Example

const serviceProxy = require('fh-mbaas-proxy');

// Creates a middleware instance that will forward all requests to a service
// specified by the given guid
const proxyMiddleware = serviceProxy({
  guid: process.env.SERVICE_GUID,
  noTrim: false
});

Full Example

A full RHMAP sample express application.

var app = require('express')();
var fh = require('fh-mbaas-api');
var mbaasExpress = fh.mbaasExpress();
var serviceProxy = require('fh-mbaas-proxy');
var port = process.env.FH_PORT || process.env.VCAP_APP_PORT || 8004;

// RHMAP routes and config
app.use('/sys', mbaasExpress.sys([]));
app.use('/mbaas', mbaasExpress.mbaas);


// Creates a middleware that will forward all requests to a service specified
// by the SERVICE_GUID environment variable
var parentProxy = serviceProxy({
  headers: {
    'x-custom-header': 'some-header-value'
  },
  guid: process.env.SERVICE_GUID,
  noTrim: false
});

// Forward all requests that lead with '/parent', e.g '/parent/child'
app.use('/parent', parentProxy);

app.use(mbaasExpress.fhmiddleware());

app.get('/ping', function (req, res) {
  res.end('pong');
});

app.use(mbaasExpress.errorHandler());

app.listen(port, function() {
  console.log('App started at: ' + new Date() + ' on port: ' + port);
});

Example

An example is included in this repo. To run it, open two terminals and cd to this repository in each. Run npm install in one of the terminals, then, in one terminal run npm run example-service, and in the other run npm run example-cloud.

The cloud should now be running on http://127.0.0.1:8001, and the service on http://127.0.0.1:8002. You can make a request through the cloud by hitting http://127.0.0.1:8001/parent/some-path-you-like which will be served by the service.

Try messing with the noTrim boolean in the example/cloud.js to see the effect it has on the request made to the service via the cloud!

Known Issues

Express Middleware

Placing the proxy after certain express middleware can cause the proxied request to timeout or receive an ECONNRESET error, or the request can simply hang to eventually timeout. It appears to be related to this issue.

You can work around this by structuring routes with nesting, or by ensuring the proxy is included before such middleware until a fix is implemented. The easiest fix is to place the proxy before all other routes if possible since it's been observed that POST/PUT will generally fail if any middleware is triggered/matched before hitting the proxy middleware.

/*
  filename: this-works.js

  This will work since the proxy middleware is before the static middleware.
 */
app.use(require('fh-mbaas-proxy')(proxyOptions));
app.use(express.static(__dirname + '/public'));

/*
  filename: this-also-works.js

  This will proxy so long as the route to be proxied does not begin with
  the path "/static" since it will then avoid the static middleware. For
  example "/ok/static" will work, but "/static/ok" will fail to proxy since
  it must pass thru the express.static middleware first
 */
app.use('/static', express.static(__dirname + '/public'));
app.use(require('fh-mbaas-proxy')(proxyOptions));


/*
  filename: this-does-not-work.js

  This won't proxy because the static middleware is run for EVERY incoming
  request BEFORE it reaches the proxy middleware.
 */
app.use(express.static(__dirname + '/public'));
app.use(require('fh-mbaas-proxy')(proxyOptions));

Options

Provided options must be an Object.

opts.headers (Optional)

Custom headers you would like to inject into a request being proxied.

opts.guid (Required)

The ServiceID/GUID of the MBaaS Service you would like to proxy requests to. This can be found on the Details tab of your MBaaS Service and is the same as the guid parameter you would pass to $fh.service calls.

opts.domain (Optional)

The domain that the target MBaaS Service is hosted on, e.g your-domain.feedhenry.com.

You only need to set this if you don't have an fhconfig.json in the root of you project. You can read about the fhconfig.json here.

opts.noTrim (Default - false)

Setting noTrim to true will use the full path for proxying, e.g if your proxy setup is like so:

app.use('/parent', serviceProxy(options));

Then a client might call "/parent/child". By default we remove the "/parent" when performing the proxy. Here's a simple full URL example of what this means:

http://cloud.fh-app.com/parent/child => http://service.fh-app.com/child

If you didn't want "/parent" getting removed then you can pass noTrim with the value, true and the URL mapping will become as follows:

http://cloud.fh-app.com/parent/child => http://service.fh-app.com/parent/child

CHANGELOG

  • 1.0.0 - Support only node.js versions above 4.4.2. Use debug for logging.
  • 0.2.1 - Fix excessive info level logs
  • 0.2.0 - Update to support fhconfig.json for configuration. Improve docs.
  • 0.1.3 - Remove 0.1.2 fixes since they are not working as expected
  • 0.1.2 - Patch to fix bug for PUT/POST requests with JSON bodies - uses restreamer
  • 0.1.1 - Patch to fix bug for PUT/POST requests with JSON bodies
  • 0.1.0 - Initial release