documentation - "Middleware"
MODIFY
README.md
README.md
-Reactive HTTP middleware framework based on <a href="http://nodejs.org" target="blank">Node.js</a> platform to make stream-based APIs development more enjoyable and simpler to write.
+Reactive and functional HTTP middleware framework based on <a href="http://nodejs.org" target="blank">Node.js</a> platform to make stream-based APIs development more enjoyable and simpler to write.
## Philosophy
*@TODO*
+The name comes from so called `marble diagrams` which are used to visually express time based behaviour of streams.
+
## Installation
*@TODO*
@@ -12,7 +14,7 @@ Reactive HTTP middleware framework based on <a href="http://nodejs.org" target="
*@TODO*
-#### Effects
+### Effects
*@TODO*
@@ -22,16 +24,43 @@ Reactive HTTP middleware framework based on <a href="http://nodejs.org" target="
### Middlewares
-*@TODO*
+Because everyting here is a stream, also plugged-in middlewares are based on simillar *Effect* interface.
+By default framework comes with composable middlewares like: logging, request body parsing.
+Below you can see how easily looks the dummy implementation of API requests logging middleware.
+
+```javascript
+export const logger$ = (request$, response) => request$
+ .pipe(
+ tap(req => console.log(`${req.method} ${req.url}`)),
+ );
+```
+
+There are two important differences compared to API Effects:
+1. stream handler takes a response object as a second argument
+2. middlewares must return stream of `request` object at the end of middleware pipeline
+
+In the example above we are taking the stream of request, tapping `console.log` side effect and returning the same
+stream as a response of our middleware pipeline. Then all you need to do is to attach the middleware to `httpListener` config.
+
+```javascript
+const middlewares = [
+ logger$,
+];
+
+export const app = httpListener({
+ middlewares,
+ effects,
+});
+```
### Custom error handling
By default **Marble.js** comes with simple and lightweight error handling middleware.
-Because *Middlewares* and *Effects* are based on the same generic interface your error
+Because *Middlewares* and *Effects* are based on the same generic interface, your error
handling middlewares works very similar to normal API *Effects*.
```javascript
-export const errorMiddleware$ = (request$, response, error) => request$
+export const error$ = (request$, response, error) => request$
.pipe(
map(req => ({
status: // ...
@@ -53,7 +82,7 @@ export const app = httpListener({
effects,
// Custom error middleware:
- errorMiddleware: customErrorMiddleware$,
+ errorMiddleware: error$,
});
```

documentation - "Middleware"