A full-featured
@macchiatojs
body parser middleware. Supportsmultipart
,urlencoded
, andjson
request bodies. Provides the same functionality as Express's bodyParser -multer
.
- 🦄 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.
# 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
.
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);
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 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']
.
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.
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
If you have any problem or suggestion please open an issue.