@binden/multipart
v2.0.2
Published
Binden middleware for parsing incoming FormData
Downloads
8
Maintainers
Readme
@binden/multipart
Binden multipart/form-data
parser middleware (based on busboy).
Installation
npm install @binden/multipart
Usage
import multipart from "@binden/multipart";
const formDataParser = multipart({ limits: { fields: 2, fileSize: 1024 } });
const next = (context) => {
if (context.request.body instanceof FormData) {
context.log.info("Body has been parsed successfully");
return context.status(200).text("GOOD");
} else {
context.log.warn("Body is not a valid `form-data`");
return context.status(400).text("BAD");
}
};
app.use("/formdata", new Router().post(formDataParser, next));
Truncated FormData
Set the throw_limits
to false
to avoid throwing errors. FormData
might be truncated due to config.limits
(See busboy
limits)
import { Multipart } from "@binden/multipart";
const formDataParser = new Multipart({
throw_limits: false,
config: {
limits: {
fieldNameSize: 5,
fieldSize: 16 * 1024,
fields: 4,
fileSize: 128 * 1024,
files: 4,
parts: 6,
},
},
});
const next = (context) => {
if (context.request.body instanceof FormData) {
context.log.info("Fields and files might have been truncated");
return context.status(200).text("MAYBE GOOD");
} else {
context.log.warn("Body is not a valid `form-data`");
return context.status(400).text("BAD");
}
};
app.use("/formdata", new Router().post(formDataParser, next));
Test
npm test