Load balancer, cache and more for blitz-js. Built on Express.js and Socket.io.
Usage
const Blitz = const Api = const blitz = blitz
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 are where our application logic goes.
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.
For further understanding, here's a simple model showing the way a request will go until we get a response:
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.
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
blitznodesapi
We recommend reading through the full docs at the 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.
blitznodesapiserverhttpapp // Native Express Middlewareblitznodesapiserversocketsio // Native Socket.io Middleware
Making requests as a client
We heavily recommend using 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.
Options
blitz
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. |