# fh-mbaas-express

> FeedHenry MBAAS Express

Latest version **6.1.2** (published 2018-11-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install fh-mbaas-express
pnpm add fh-mbaas-express
yarn add fh-mbaas-express
bun add fh-mbaas-express
```

## 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 | 6.1.2 |
| Published | 2018-11-01 |
| First published | 2014-04-07 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 11 |
| Unpacked size | 1.8 MB |
| Known vulnerabilities | 0 (+15 in 5 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | craigbrookes, damurphy, davidffrench, feedhenry, feedhenry-dev, grdryn, jasonmadigan, maleck13, matzew, omatskiv, pb82, tjackman-rh, weileylee, ziccardi |

## Links

- npm: https://www.npmjs.com/package/fh-mbaas-express
- Repository: https://github.com/feedhenry/fh-mbaas-express
- Homepage: https://github.com/feedhenry/fh-mbaas-express#readme
- Issues: https://github.com/feedhenry/fh-mbaas-express/issues
- npm.io page: https://npm.io/package/fh-mbaas-express

## Dependencies (11)

- [cors](https://npm.io/package/cors.md) 2.1.0
- [async](https://npm.io/package/async.md) 0.2.9
- [multer](https://npm.io/package/multer.md) 1.3.0
- [express](https://npm.io/package/express.md) 4.16.0
- [request](https://npm.io/package/request.md) 2.88.0
- [type-is](https://npm.io/package/type-is.md) ~1.2.1
- [fh-amqp-js](https://npm.io/package/fh-amqp-js.md) 0.7.5
- [underscore](https://npm.io/package/underscore.md) 1.5.1
- [body-parser](https://npm.io/package/body-parser.md) 1.18.2
- [fh-mbaas-client](https://npm.io/package/fh-mbaas-client.md) 1.1.4
- [fh-reportingclient](https://npm.io/package/fh-reportingclient.md) 1.0.1

## Recent versions

- 6.1.2 (latest) — 2018-11-01
- 6.0.3 (development) — 2018-08-24
- 5.4.3 (RHMAP-5324) — 2016-03-29
- 5.3.9 (RHM3821) — 2016-01-22
- 5.3.7 (RHM3565_3) — 2016-01-06
- 5.3.6 (RHM3565) — 2015-12-18
- 5.3.5 (RHM2618lst) — 2015-12-09
- 5.3.4 (RHM2618l) — 2015-12-09
- 5.3.3 (RHM2618b) — 2015-12-08
- 5.3.2 (version4.0c) — 2015-11-13
- 5.3.1 (version4.0b) — 2015-11-10
- 5.3.0 (version4.0) — 2015-11-09
- 6.1.1 — 2018-09-27
- 6.1.0 — 2018-09-19
- 6.0.2 — 2018-08-23
- … 82 more at https://npm.io/package/fh-mbaas-express/versions

## README

fh-mbaas-express
================

[![npm package](https://nodei.co/npm/fh-mbaas-express.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/fh-mbaas-express/)

[![Build status](https://img.shields.io/travis/feedhenry/fh-mbaas-express/master.svg?style=flat-square)](https://travis-ci.org/feedhenry/fh-mbaas-express)
[![Dependency Status](https://img.shields.io/david/feedhenry/fh-mbaas-express.svg?style=flat-square)](https://david-dm.org/feedhenry/fh-mbaas-express)
[![Known Vulnerabilities](https://snyk.io/test/npm/fh-mbaas-express/badge.svg?style=flat-square)](https://snyk.io/test/npm/fh-mbaas-express)

fh-mbaas-express is the FeedHenry MBaaS running on top of [Express](http://expressjs.com/).

|                 | Project Info  |
| --------------- | ------------- |
| License:        | Apache License, Version 2.0  |
| Build:          | npm  |
| Documentation:  | http://docs.feedhenry.com/v3/api/cloud_api.html  |
| Issue tracker:  | https://issues.jboss.org/projects/FH/summary  |
| Mailing list:   | [feedhenry-dev](https://www.redhat.com/archives/feedhenry-dev/) ([subscribe](https://www.redhat.com/mailman/listinfo/feedhenry-dev))  |
| IRC:            | [#feedhenry](https://webchat.freenode.net/?channels=feedhenry) channel in the [freenode](http://freenode.net/) network.  |

# Usage

Add the following to the 'dependencies' section of the app's **'package.json'** file:

    "fh-mbaas-express" : "~5.6.1",
    "express" : "~4.14.0"

Add a file to the app's **'application.js'**, with the following contents:

```javascript
var express = require('express');
var mbaasExpress = require('fh-mbaas-express');
var cors = require('cors');

// list the endpoints which you want to make securable here
var securableEndpoints;
// fhlint-begin: securable-endpoints
securableEndpoints = ['/hello'];
// fhlint-end

var app = express();

// Enable CORS for all requests
app.use(cors());

// Note: the order which we add middleware to Express here is important!
app.use('/sys', mbaasExpress.sys(securableEndpoints));
app.use('/mbaas', mbaasExpress.mbaas);

// Note: important that this is added just before your own Routes
app.use(mbaasExpress.fhmiddleware());

// allow serving of static files from the public directory
app.use(express.static(__dirname + '/public'));

// fhlint-begin: custom-routes
app.use('/', require('./lib/cloud.js')());
// fhlint-end

// Important that this is last!
app.use(mbaasExpress.errorHandler());

var port = process.env.FH_PORT || process.env.VCAP_APP_PORT || 8001;
var server = app.listen(port, function() {
  console.log("App started at: " + new Date() + " on port: " + port);
});
```

When running on the [FeedHenry](http://www.feedhenry.com/) platform, this module should not be used directly. Instead, [fh-mbaas-api](https://github.com/fheng/fh-mbaas-api) module should be used and an instance of fh-mbaas-express cab be obtained via fh-mbaas-api. E.g.

```javascript
var mbaasApi = require('fh-mbaas-api');
var mbaasExpress = mbaasApi.mbaasExpress();
```

## Customising & Extending
The above application.js is just an [Expressjs application](http://expressjs.com/api.html) - it's easily extensible.

### Custom APIs
You can create custom API handlers in the Express format by doing:

    app.use('/myapi', function(req, res){
      res.end('My custom response');
    });

See [Express Router](http://expressjs.com/4x/api.html#router) for more information.

### Serving Static Files
Express has a built-in static file server. In this example, we host files under the public directory:  

    app.use(express.static(__dirname + '/public'));


# REST API


## Cloud

### (POST | GET | PUT | DELETE) /:someFunction
**Authentication** : Optional - can be enabled globally or on a per-endpoint basis under "Endpoints" section of the studio.
**Response formats** : JSON, binary, plain text

### Headers
**x-fh-auth-app** : API key of your application, found under the "details" section of the studio.

### Body:
JSON format - paramaters to be passed to the exported function, see 'Writing API Functions'.
### Response:
Result as passed to the callback function of the exported function - see 'Writing API Functions'.


### Writing API functions

See [Express Router](http://expressjs.com/4x/api.html#router) for more information.

```javascript
function helloRoute() {
  var hello = new express.Router();
  hello.use(bodyParser());

  // GET REST endpoint - query params may or may not be populated
  hello.get('/hello', function(req, res) {
    var world = req.query ? req.query.hello : 'World';

    res.writeHead(200, {"Content-Type":"application/json"});
    res.end(JSON.stringify({msg: 'Hello ' + world}));
  });
  return hello;
}

```


## mBaaS
### POST /mbaas/db
**Authentication** : Required - App API key goes here.
**Response format** : JSON

### Headers
**x-fh-auth-app** : API key of your application, found under the "details" section of the studio.

### POST Body:
JSON body - same as $fh.db params. A summary of body options follows - For more, see [$fh.db docs](http://docs.feedhenry.com/v2/api_cloud_apis.html#$fh.db)

    {
        "act": "create|update|list|delete|deleteall",
        "type": "collectionName",
        "guid": "GUID of object operating on - not required for list or deleteall",
        "fields": "JSON definition of fields - required for create|update",
        "eq|ne|in" : "JSON definition of query to match - supported for list only"
    }

### Response:
As per [$fh.db](http://docs.feedhenry.com/v2/api_cloud_apis.html#$fh.db)

## Sys
## GET /sys/info/ping
**Authentication** : None
**Response formats** : Plaintext

### Headers
None

### Response:
    "OK"
if application is running as expected. Will respond with a 404 otherwise

## GET /sys/info/endpoints
**Authentication** : None
**Response formats** : JSON

### Headers
None

### Response:
    {
      endpoints : ['array of endpoints exported as public functions']
    }

## GET /sys/info/memory
**Authentication** : None
**Response formats** : JSON

### Headers
None

### Response:
    {
      rss: 13721600, // Resident set size
      heapTotal: 7195904, // V8's total available memory
      heapUsed: 2369744  // V8's used memory
    }

## GET /sys/info/memory
**Authentication** : None
**Response formats** : Plaintext

### Headers
None

### Response:
    0.1.0

# Backward compatability with main.js

If you want to use the older fh-nodeapp/fh-webapp style main.js, you need to change the /cloud endpoint in your application.js as follows:

```
var mainjs = require('./lib/main.js');
app.use('/cloud', mbaas.cloud(mainjs));
```

Additionally, you need to ammend the call to `sys` for secure endpoints:

```
app.use('/sys', mbaas.sys(securableEndpoints, mainjs));
```

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