@flitz/body
v3.0.0
Published
flitz middleware for parsing and handling request bodies.
Downloads
41
Maintainers
Readme
@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