LogoLogo
ChangelogGitHubTwitterGitter
v3.x
v3.x
  • Marble.js
  • Getting started
    • Installation
    • Quick setup
  • HTTP
    • Effects
    • Middlewares
    • Routing
    • Errors
    • Output
    • Context
    • Advanced
      • Logging
      • Validation
      • Server Events
      • Streaming
      • Continuous mode
  • Messaging
    • Core concepts
      • Events
      • Effects
    • Microservices
      • AMQP (RabbitMQ)
      • Redis Pub/Sub
    • CQRS
    • WebSockets
  • Testing
    • HTTP routes testing
  • Other
    • How does it glue together?
    • Migration guides
      • Migration from version 2.x
      • Migration from version 1.x
    • API reference
      • core
        • bindTo
        • bindEagerlyTo
        • createEvent
        • createServer
        • combineRoutes
        • createContextToken
        • EffectFactory
        • r.pipe
        • httpListener
        • operator: matchEvent
        • operator: use
        • operator: act
      • messaging
        • eventBus
        • messagingClient
        • createMicroservice
        • reply
      • websockets
        • webSocketListener
        • operator: broadcast
        • operator: mapToServer
      • middleware-multipart
      • middleware-cors
      • middleware-joi
      • middleware-jwt
        • Token signing
      • middleware-io
      • middleware-logger
      • middleware-body
    • Style Guide
    • FAQ
Powered by GitBook
On this page
  1. HTTP

Output

Every Marble.js listener factory allows you to intercept outgoing messages via dedicated output$ handler.

Building your own output effect

Using HttpOutputEffect you can grab an outgoing HTTP response and map it into different response, modifying outgoing data on demand.

HttpOutputEffect :: Observable<{ req: HttpRequest; res: HttpEffectResponse }>
  -> Observable<HttpEffectResponse>

HttpOutputEffect allows you to grab the outgoing response together with corresponding initial request. Lets build a simple response compression middleware.

import { HttpOutputEffect } from '@marblejs/core';
import { map } from 'rxjs/operators';
import * as zlib from 'zlib';

const output$: HttpOutputEffect = res$ =>
  res$.pipe(
    map(({ res, req }) =>  {
      switch(req.headers['accept-encoding']) {
        case 'br':
          return ({
            ...res,
            headers: { ...res.headers, 'Content-Encoding': 'br' },
            body: res.body.pipe(zlib.createBrotliDecompress()),
          });
        case 'gzip':
          return ({
            ...res,
            headers: { ...res.headers, 'Content-Encoding': 'gzip' },
            body: res.body.pipe(zlib.createGunzip()),
          });
        case 'deflate':
          return ({
            ...res,
            headers: { ...res.headers, 'Content-Encoding': 'deflate' },
            body: res.body.pipe(zlib.createInflate()),
          });
        default:
          return res;
      }
    }),
  );

To connect the output effect, all you need to do is to attach it to output$ property in httpListener config object.

import { httpListener } from '@marblejs/core';
import { output$ } from './output.effect';

export const listener = httpListener({
  middlewares: [ ... ],
  effects: [ ... ],
  output$, // 👈
});
PreviousErrorsNextContext

Last updated 5 years ago