Beautify body parser JSON syntax error
npm install bodyparser-json-error --save
var express = require('express'); var bodyParser = require('body-parser'); var bodyParserError = require('bodyparser-json-error'); var app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); // Beautify body parser json syntax error app.use(bodyParserError.beautify());
You can optionally set the status code and response for the body parser syntax error.
app.use(bodyParserError.beautify({status: 500 , res: {msg: 'You sent a bad JSON !'}}));
The output:
HTTP Status Code: 500
{ "msg": "You sent a bad JSON !" }
- status: The response status code. Default: 400
- res: The response body. Default:
{msg: 'Invalid JSON'}
To run the test suite, first install the dependencies, then run npm test
:
$ npm install $ npm test
MIT License