# blitz-js-api

> ⚡ API Node for the blitz-js framework

Latest version **1.0.8** (published 2018-04-01) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install blitz-js-api
pnpm add blitz-js-api
yarn add blitz-js-api
bun add blitz-js-api
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2018-04-01 |
| First published | 2017-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 10 |
| Unpacked size | 27.4 KB |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | kaptard, nakroma |

## Links

- npm: https://www.npmjs.com/package/blitz-js-api
- Repository: https://github.com/nexus-devs/blitz.js-api
- Homepage: https://github.com/nexus-devs/blitz.js-api#readme
- Issues: https://github.com/nexus-devs/blitz.js-api/issues
- npm.io page: https://npm.io/package/blitz-js-api

## Dependencies (10)

- [mime](https://npm.io/package/mime.md) ^2.2.2
- [chalk](https://npm.io/package/chalk.md) ^2.3.2
- [redis](https://npm.io/package/redis.md) ^2.8.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.5
- [express](https://npm.io/package/express.md) ^4.16.3
- [socket.io](https://npm.io/package/socket.io.md) ^2.1.0
- [body-parser](https://npm.io/package/body-parser.md) ^1.18.2
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.2.0
- [circular-json](https://npm.io/package/circular-json.md) ^0.5.1
- [async-middleware-stack](https://npm.io/package/async-middleware-stack.md) ^1.0.1

## Recent versions

- 1.0.8 (latest) — 2018-04-01
- 1.0.7 — 2018-03-25
- 1.0.6 — 2018-03-23
- 1.0.5 — 2018-03-21
- 1.0.4 — 2018-03-04
- 1.0.3 — 2018-02-25
- 1.0.2 — 2018-02-23
- 1.0.0 — 2018-02-23
- 0.5.13 — 2018-01-07
- 0.5.12 — 2018-01-07
- 0.5.11 — 2018-01-06
- 0.5.10 — 2017-12-16
- 0.5.9 — 2017-12-08
- 0.5.8 — 2017-12-08
- 0.5.7 — 2017-12-07
- … 96 more at https://npm.io/package/blitz-js-api/versions

## README

[![blitz-js-api](https://i.imgur.com/rtmexse.png)](https://github.com/nexus-devs)

##

<p align='center'>Load balancer, cache and more for <a href='https://github.com/nexus-devs/blitz-js'>blitz-js</a>. Built on Express.js
and Socket.io.</p>

<br>
<br>


## Usage
```js
const Blitz = require('blitz-js')
const Api = require('blitz-js-api')
const blitz = new Blitz()

blitz.use(new Api(options))
```
This will open a web server on `localhost:3003` which serves data from connected
blitz-js-core nodes. No further setup needed - the [core nodes](https://github.com/nexus-devs/blitz-js-core) are where our application logic goes.

<br>


## How does it work?
At its core, blitz-js-api is a load balancer for connected blitz-js-core nodes.
What makes it special is that it allows the use of custom connection adapters
that create a common `req` and `res` object from any connection type. (HTTP &
Socket.io by default)

This way our middleware functions and routed endpoints will work for *all*
connection types, with no need to adjust them individually.

<br>

For further understanding, here's a simple model showing the way a request
will go until we get a response:

[![model](https://i.imgur.com/JjUKPuk.png)](https://i.imgur.com/JjUKPuk.png)

This is only one half of the way a request goes. To see what happens once the request
is sent to a connected core node, check out [blitz-js-core](https://github.com/nexus-devs/blitz-js-core).

<br>

## Writing custom middleware
If you need to access the `req`, `res` objects before they're sent to the
core node, you can simply add your custom function to the async middleware
stack. It behaves much like express middleware, but takes advantage of ES7
async.

### Example
```js
blitz.nodes.api.use('/ferret', async (req, res) => {

  // Return image of angry ferret if the user isn't tobi.
  if (req.user.uid !== 'tobi') {
    let image = await getSomeAngryFerretPictures()

    // we MUST return a truthy value to stop the middleware chain from executing
    return res.send(image)
  }

  // If nothing is returned, we'll assume the user is tobi and proceed with the
  // next middleware function
})
```
We recommend reading through the full docs at the [async-middleware-stack](https://github.com/Kaptard/async-middleware-stack)
repo if you need further information.

### Native Middleware
If necessary, you can still add native connection middleware which runs before
our own.
```js
blitz.nodes.api.server.http.app.use((req, res, next) => {}) // Native Express Middleware
blitz.nodes.api.server.sockets.io.use((socket, next) => {}) // Native Socket.io Middleware
```

<br>

## Making requests as a client
We heavily recommend using [blitz-js-query](https://github.com/nexus-devs/blitz-js-query)
since it takes care of authorization, rate limits and potential downtimes automatically.
This package is also used to connect core nodes to API nodes, so we most likely
won't be slacking with its maintenance.

<br>

## Options

```js
blitz.use(new Api(options))
```

| Option        | Default       | Description   |
|:------------- |:------------- |:------------- |
| port   | `3003`   | Port to listen on for requests. |
| redisUrl | `'redis://localhost'` | Base URL for redis connection. |
| cacheDb | `1` | Redis database used to store cache data. |
| cacheExp | `10` | Time in seconds until cached data expires when no explicit duration is specified. |
| requestTimeout | `1000` | Time to wait in ms when sending request to core node before assuming timeout. |
| routes | `'/connections/entry/routes.js'` | Entry point for HTTP requests via express. (No need to modify unless you're building something very exotic.) |
| events | `'/connections/entry/events.js'` | Entry point for WS requests via Socket.io. |

<br>

## License
[MIT](/LICENSE.md)

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