body-parser-ext
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

body-parser-ext

npm

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(
  (
    errany,
    _express.Request,
    resexpress.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}`));

License

MIT

Package Sidebar

Install

npm i body-parser-ext

Weekly Downloads

32

Version

1.0.0

License

MIT

Unpacked Size

6.76 kB

Total Files

5

Last publish

Collaborators

  • tumbumer