body-parser-ext
v1.0.0
Published
Extended Node.js body parsing middleware
Downloads
478
Maintainers
Readme
body-parser-ext
An extended version of body-parser middleware with additional error handling using VEerror
Extended error handling
- ERR_BODY_PARSE_JSON
- ERR_PAYLOAD_TOO_LARGE
Installation
npm i -S body-parser-ext
Usage
As well as body-parser
Javascript example
'use strict';
const express = require('express');
const bodyParser = require('body-parser-ext');
const PORT = 3000;
const app = express();
// Parse application/json
app.use(bodyParser.json());
// Sample middleware
app.get('/', (req, res) => {
res.status(200).json(req.body);
});
// Other middlewares
// The catch 404 error handler
app.use((_, res) => {
res.status(404).end();
});
// The 'catch-all' error handler
app.use((err, _, res, __) => {
// Bad request
if (err.name === bodyParser.ERR_BODY_PARSE_JSON) {
console.log(bodyParser.ERR_BODY_PARSE_JSON);
return res.status(400).end();
}
// Payload too large
if (err.name === bodyParser.ERR_PAYLOAD_TOO_LARGE) {
console.log(bodyParser.ERR_PAYLOAD_TOO_LARGE);
return res.status(413).end();
}
// Unexpected error
console.log('unexpected error');
res.status(500).end();
});
// Start server
app.listen(PORT, () => console.log(`Example app listening on port ${PORT}`));
Typescript example
import express from 'express';
import bodyParser from 'body-parser-ext';
const PORT = 3000;
const app = express();
// Parse application/json
app.use(bodyParser.json());
// Sample middleware
app.get('/', (req: express.Request, res: express.Response) => {
res.status(200).json(req.body);
});
// Other middlewares
// The catch 404 error handler
app.use((_: express.Request, res: express.Response) => {
res.status(404).end();
});
// The 'catch-all' error handler
app.use(
(
err: any,
_: express.Request,
res: express.Response,
__: express.NextFunction
) => {
// Bad request
if (err.name === bodyParser.ERR_BODY_PARSE_JSON) {
console.log(bodyParser.ERR_BODY_PARSE_JSON);
return res.status(400).end();
}
// Payload too large
if (err.name === bodyParser.ERR_PAYLOAD_TOO_LARGE) {
console.log(bodyParser.ERR_PAYLOAD_TOO_LARGE);
return res.status(413).end();
}
// Unexpected error
console.log('unexpected error');
res.status(500).end();
}
);
// Start server
app.listen(PORT, () => console.log(`Example app listening on port ${PORT}`));