http-middleware-metalab
Minimalist, composable http middleware pack for http/express/hapi.
Packs
- base - Some sensible defaults.
- webpack - For serving assets from webpack-based projects.
- react - Server-side rendering with react and redux.
Usage
Install http-middleware-metalab
and add it to your package.json
file:
npm install --save http-middleware-metalab
These middleware components are NOT the same as express middleware; they are conceptually designed in a manner more similar to redux stores. Every middleware is an object with properties corresponding to events on an http.Server
object; e.g. request
, error
, upgrade
, etc. Each middleware function takes an existing middleware object and composes it.
For example, adding a req.message
field:
{ // Take in the existing middleware here return { const request = middleware; // Return a new middleware here. return ...middleware { reqmessage = message; // Composition! ; } ; };} // Create the composed middleware.const app = // Your "base" middleware. { resstatusCode = 200; res; }; // Create the server and use the appropriate methods on the middleware object.const server = http;server;server;
In that sense http middleware is even less opinionated than express middleware. It is merely a composition mechanism for a previously non-composable set of functions (http server events).
http
With ;;; const server = http;const createApp = ;const app = ; ;
express
With ;;; const createMiddleware = ;const app = ; app;app;
hapi
With ;;; const createMiddleware = ;const server = ; server;server;serverstart;