middleware-body

HTTP request body parser middleware for Marble.js.

Installation

$ npm i @marblejs/middleware-body

Requires @marblejs/core to be installed.

Importing

import { bodyParser$ } from '@marblejs/middleware-body';

Usage

app.ts
import { bodyParser$ } from '@marblejs/middleware-body';

const middlewares = [
  bodyParser$,
  ...
];

const effects = [
  ...
];

export const app = httpListener({ middlewares, effects });

Lets assume that we have the following CURL command, which triggers POST api/login endpoint:

$ curl --header "Content-Type: application/json" \
  --request POST \
  --data '{ "username": "foo", "password": "bar" }' \
  http://localhost:3000/api/login

Using previously connected bodyParser$ middleware, the app will intercept the following payload object:

req.body = {
  username: 'foo',
  password: 'bar',
};

where the POST request body can be intercepted inside sample Effect like follows:

dummyLogin.effect.ts
const dummyLogin$ = EffectFactory
  .matchPath('/login')
  .matchType('POST')
  .use(req$ => req$.pipe(
    map(req => req.body)
    map(body => ({ body: `Hello, ${body.username}!` }))
  );

All properties and values in req.body object are untrusted and should be validated before trusting.

This middleware does not handle multipart bodies.

Last updated