@binden/body
v2.0.1
Published
Binden body parser middleware
Downloads
10
Readme
@binden/body
Binden body parser middleware. Supports the following content types
application/json
application/x-www-form-urlencoded
plain/text
and any combination of the following encodings
br
x-gzip
gzip
deflate
Installation
npm install @binden/body
Usage
import BodyParser from "@binden/body";
class MyMiddleware extends Middleware {
public run(context: Context): void {
const { body } = context.request;
context.log.info("Body has been parsed (or not)", { body });
}
}
app.use("/deposit", new Router().post(BodyParser, new MyMiddleware()));
Custom parse
function
import { Binden } from "binden";
import AJV, { JTDDataType } from "ajv/dist/jtd.js";
import BodyParser from "@binden/body";
const ajv = new AJV();
const schema = {
properties: { username: { type: "string" }, password: { type: "string" } },
additionalProperties: false,
};
const parse = ajv.compileParser<JTDDataType<typeof schema>>(schema);
const body_parser = new BodyParser({ parse });
const echo = (context) => {
const { body } = context.request;
context.log.info("Body has been parsed (or not)", { body });
return context.json(body);
};
new Binden()
.use("/login", new Router().post(BodyParser, echo))
.createServer()
.listen(port, () => {
console.log(`Server is listening on the port ${port}`);
});
Test
npm test