@flitz/body
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

npm supported flitz version last build PRs Welcome

@flitz/body

A body parser and handler middleware for flitz.

Install

Run

npm install --save @flitz/body

from the folder, where your package.json is stored.

Usage

const flitz = require('flitz');
const body = require('@flitz/body');

const run = async () => {
  const app = flitz();

  //             👇👇👇
  app.post('/', [ body() ], async (req, res) => {
    res.write('Your body as string from buffer: ' + req.body.toString());
    res.end();
  });

  await app.listen(3000);
};

run();

Or the TypeScript way:

import flitz from 'flitz';
import { body } from '@flitz/body';

const run = async () => {
  const app = flitz();

  //             👇👇👇
  app.post('/', [ body() ], async (req, res) => {
    res.write('Your body as string from buffer: ' + req.body.toString());
    res.end();
  });

  await app.listen(3000);
};

run();

JSON

import flitz from 'flitz';
import { json } from '@flitz/body';

const run = async () => {
  const app = flitz();

  //             👇👇👇
  app.post('/', [ json() ], async (req, res) => {
    res.write('Your JSON body as JavaScript object: ' + JSON.stringify(req.body, null, 2));
    res.end();
  });

  await app.listen(3000);
};

run();

YAML

import flitz from 'flitz';
import { yaml } from '@flitz/body';

const run = async () => {
  const app = flitz();

  //             👇👇👇
  app.post('/', [ yaml() ], async (req, res) => {
    res.write('Your YAML body as JavaScript object: ' + JSON.stringify(req.body, null, 2));
    res.end();
  });

  await app.listen(3000);
};

run();

Form

import flitz from 'flitz';
import { form } from '@flitz/body';

const run = async () => {
  const app = flitz();

  //             👇👇👇
  app.post('/', [ form() ], async (req, res) => {
    res.write('Your body as key / value pair object: ' + JSON.stringify(req.body, null, 2));
    res.end();
  });

  await app.listen(3000);
};

run();

String

import flitz from 'flitz';
import { string } from '@flitz/body';

const run = async () => {
  const app = flitz();

  //              👇👇👇
  app.post('/', [ string() ], async (req, res) => {
    res.write('Your body as UTF-8 string: ' + req.body);
    res.end();
  });

  await app.listen(3000);
};

run();

Maximum size

//                    👇👇👇 (128 MB)
app.post('/', [ body({ max: 128 * 1024 * 1024 }) ], async (req, res) => {
  // your code here
});

TypeScript

TypeScript is optionally supported. The module contains its own definition files.

Credits

The module makes use of:

License

MIT © Marcel Kloubert

Package Sidebar

Install

npm i @flitz/body

Weekly Downloads

1

Version

3.0.0

License

MIT

Unpacked Size

26.5 kB

Total Files

13

Last publish

Collaborators

  • mkloubert
  • mkloubertego