@macchiatojs/body
v0.7.0
Published
A full-featured macchiatojs body parser (with raw Node.js support) ๐ณ.
Downloads
12
Maintainers
Readme
@macchiatojs/body
A full-featured
@macchiatojs
body parser middleware. Supportsmultipart
,urlencoded
, andjson
request bodies. Provides the same functionality as Express's bodyParser -multer
.
Features
- ๐ฆ Based on top of [co-body] and [formidable].
- ๐ Isomorphic to the moon.
- ๐ฅ Blaze and lightweight parser.
- ๐ Support for
form
. - ๐ฏ Support for
json
. - ๐ฅ Support for
multipart
. - ๐ช Support for
file upload
. - ๐ Support for
text
(raw text, html, xml). - โจ Asynchronous support (
async/await
). - ๐ข Raw Node.js (
http
) support. - ๐ TypeScript support.
Installation
# npm
$ npm install @macchiatojs/body
# yarn
$ yarn add @macchiatojs/body
When use this module with raw Node.js should insall an additional module
type-is
.
Usage
with Macchiato.js
import Macchiato from "@macchiatojs/kernel";
import requestBody from "@macchiatojs/body";
const app = new Macchiato();
app.use(requestBody(bodyOpts));
app.use((request: Request, response: Response) => {
response.body = request["body"];
});
app.start(1111);
with raw Node.js
import http from "http";
import requestBody from "@macchiatojs/body";
const server = http.createServer(async (request, response) => {
try {
await requestBody()(request);
response.statusCode = 200;
response.write(request?.body);
response.end();
return;
} catch (error) {
response.statusCode = 500;
response.end("some thing long ...");
return;
}
});
server.listen(1111);
Note
If you want to use [email protected]
you should replace you're import from
import requestBody from "@macchiatojs/body"
to
import requestBody from "@macchiatojs/body/v1"
When we release the
1.0.0
we will drop support for[email protected]
.
Options
Options available for
@macchiatojs/body
. Four custom options, and others are fromraw-body
andformidable
.
expressify
{Boolean} Only withMacchiato.js
; Choose the right middleware style (false ==> koaify / true ==> expressify), defaulttrue
jsonLimit
{String|Integer} The byte (if integer) limit of the JSON body, default1mb
formLimit
{String|Integer} The byte (if integer) limit of the form body, default56kb
textLimit
{String|Integer} The byte (if integer) limit of the text body, default56kb
encoding
{String} Sets encoding for incoming form fields, defaultutf-8
multipart
{Boolean} Parse multipart bodies, defaultfalse
urlencoded
{Boolean} Parse urlencoded bodies, defaulttrue
text
{Boolean} Parse text bodies, such as XML, defaulttrue
json
{Boolean} Parse JSON bodies, defaulttrue
jsonStrict
{Boolean} Toggles co-body strict mode; if set to true - only parses arrays or objects, defaulttrue
formidable
{Object} Options to pass to the formidable multipart parserparsedMethods
{String[]} Declares the HTTP methods where bodies will be parsed, default['POST', 'PUT', 'PATCH']
.
A note about parsedMethods
see http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#section-6.3
GET
,HEAD
, andDELETE
requests have no defined semantics for the request body, but this doesn't mean they may not be valid in certain use cases.- @macchiatojs/body is strict by default, parsing only
POST
,PUT
, andPATCH
requests.
Some options for formidable
See node-formidable for a full list of options
maxFields
{Integer} Limits the number of fields that the querystring parser will decode, default1000
maxFieldsSize
{Integer} Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default2mb (2 * 1024 * 1024)
uploadDir
{String} Sets the directory for placing file uploads in, defaultos.tmpDir()
keepExtensions
{Boolean} Files written touploadDir
will include the extensions of the original files, defaultfalse
hashAlgorithm
(hash
with [email protected]) {String} If you want checksums calculated for incoming files, set this to either'sha1'
or'md5'
, defaultfalse
multiples
{Boolean} Multiple file uploads or no, defaulttrue
onFileBegin
{Function} Special callback on file begin. The function is executed directly by formidable. It can be used to rename files before saving them to disk. See the docs
Support
If you have any problem or suggestion please open an issue.
License
MIT ยฉ Imed Jaberi