LogoLogo
ChangelogGitHubTwitterGitter
v2.x
v2.x
  • Introduction
  • Overview
    • Getting started
    • Effects
    • Routing
    • Middlewares
    • Error handling
    • How does it glue​ together?
  • Advanced
    • Context
    • Server events
    • Validation
    • Streaming
    • Output interceptor
  • WebSockets
    • Getting started
    • Effects
    • Middlewares
    • Error handling
    • Connections handling
  • API Reference
    • core
      • bindTo
      • createServer
      • combineRoutes
      • createContextToken
      • EffectFactory
      • r.pipe
      • httpListener
      • operator: matchEvent
      • operator: use
    • websockets
      • webSocketListener
      • operator: broadcast
      • operator: mapToServer
    • middleware-body
    • middleware-logger
    • middleware-io
    • middleware-jwt
      • Token signing
    • middleware-joi
    • middleware-cors
    • middleware-multipart
  • Other
    • Migration from version 1.x
    • Changelog
    • FAQ
Powered by GitBook
On this page
  • Hello, world!
  • HttpRequest
  • HttpResponse
  1. Overview

Effects

PreviousGetting startedNextRouting

Last updated 5 years ago

Hello, world!

Effect is the main building block of the whole framework. It is just a function which returns a stream of events. Using its generic interface we can define API endpoints, , and much more (see next chapters). Let's define our first "hello world" HttpEffect!

const helloEffect$: HttpEffect = req$ => req$.pipe(
  mapTo({ body: 'Hello, world!' }),
);

The Effect above responds to incoming request with Hello, world! message. In Marble.js, every Effect tries to be referentailly transparent, which means that each incoming request has to be mapped to an object with attributes like body, status or headers. If the status code or headers are not defined, then the API by default responds with 200 status and application/json header.

In order to route our first Effect, we have to define the path and HTTP method that the incoming request should be matched to. The simplest implementation of an HTTP API endpoint can look like this.

const hello$ = r.pipe(
  r.matchPath('/'),
  r.matchType('GET'),
  r.useEffect(helloEffect$));
const hello$ = EffectFactory
  .matchPath('/')
  .matchType('GET')
  .use(req$ => req$.pipe(
    mapTo({ body: `Hello, world!` })
  ));

Lets define a little bit more complex endpoint.

const postUser$ = r.pipe(
  r.matchPath('/user'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    map(req => req.body as User),
    mergeMap(Dao.postUser),
    map(response => ({ body: response }))
  )));
const postUser$ = EffectFactory
  .matchPath('/user')
  .matchType('POST')
  .use(req$ => req$.pipe(
    map(req => req.body),
    mergeMap(Dao.postUser),
    map(response => ({ body: response }))
  );

HttpRequest

  • url

  • method

  • headers

HttpResponse

The example above will match every POST request that matches to /user url. Using previously parsed POST body (see middleware) we can map it to example DAO which returns a HttpEffectResponse object as an action confirmation.

Since Marble.js 2.0, you can build HTTP API routes using builder or using new, pipeable functions inside function.

Every HttpEffect has an access to two most basics objects created by http.Server. HttpRequest is an abstraction over the basic Node.js object. It may be used to access response status, headers and data, but in most scenarios you don't have to deal with all available APIs offered by IncomingMessage class. The most common properties available in request object are:

body (see section)

params (see chapter)

query (see chapter)

For more details about available API offered in http.IncomingMessage, please visit .

Like the previously described HttpRequest, the HttpResponse object is also an abstraction over basic Node.js object. Besides the default API, the response object exposes an res.send method, which can be a handy wrapper over Marble.js responding mechanism. For more information about the res.send method, visit chapter.

For more details about the available API offered in http.ServerResponse, please visit .

middlewares
error handlers
bodyParser$
EffectFactory
r.pipe
http.IncomingMessage
bodyParser$
routing
routing
official Node.js docummentation
official Node.js docummentation
http.ServerResponse
Middlewares