Skip to content

Latest commit

 

History

History
53 lines (39 loc) · 1.63 KB

README.md

File metadata and controls

53 lines (39 loc) · 1.63 KB

Yttrium Body Parser

npm version

This package wraps the Express body-parser package to make it usable by the YttriumJS web framework.

Installation

npm install yttbody --save

How To Use

The Yttrium server exposes the use method to add plugins. Add the body parsing plugin like this:

const Y = require('yttrium-server');
const parseBody = require('yttbody');

const { $, server, router } = Y();
$(parseBody).use();

This will expose a parseBody method on the jQuery object (regardless of the name used when importing the body parser). To parse a request body, use the parseBody method in one of your route handlers:

$.route('index')
  .append('<post-example>');

$.route('post-example')
    .on('route', (e, req, res) => {
      e.stopPropagation();
      $(req).parseBody('json')
        .then(body => res.end(JSON.stringify(body)))
        .catch(e => res.end(JSON.stringify(e)));
    });

API

$(req).parseBody(parser, options)

parser is one of the four parsers supported by body-parser:

  • json
  • raw
  • text
  • urlencoded

options accepts any parser-specific options as documented in the body-parser README.

Note: body-parser has deprecated the use of urlencoded without the { extended: true } option set. This package auto sets { extended: true } when using the urlencoded parser.

License

MIT