0.7.0 • Published 4 years ago
@macchiatojs/body v0.7.0
@macchiatojs/body
A full-featured
@macchiatojsbody parser middleware. Supportsmultipart,urlencoded, andjsonrequest 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/bodyWhen 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 formidable@v1.x.x 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.0we will drop support forformidable@v1.x.x.
Options
Options available for
@macchiatojs/body. Four custom options, and others are fromraw-bodyandformidable.
expressify{Boolean} Only withMacchiato.js; Choose the right middleware style (false ==> koaify / true ==> expressify), defaulttruejsonLimit{String|Integer} The byte (if integer) limit of the JSON body, default1mbformLimit{String|Integer} The byte (if integer) limit of the form body, default56kbtextLimit{String|Integer} The byte (if integer) limit of the text body, default56kbencoding{String} Sets encoding for incoming form fields, defaultutf-8multipart{Boolean} Parse multipart bodies, defaultfalseurlencoded{Boolean} Parse urlencoded bodies, defaulttruetext{Boolean} Parse text bodies, such as XML, defaulttruejson{Boolean} Parse JSON bodies, defaulttruejsonStrict{Boolean} Toggles co-body strict mode; if set to true - only parses arrays or objects, defaulttrueformidable{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, andDELETErequests 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, andPATCHrequests.
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, default1000maxFieldsSize{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 touploadDirwill include the extensions of the original files, defaultfalsehashAlgorithm(hashwith formidable@v1.x.x) {String} If you want checksums calculated for incoming files, set this to either'sha1'or'md5', defaultfalsemultiples{Boolean} Multiple file uploads or no, defaulttrueonFileBegin{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.