An utility class to generate and handle RESTful api errors in web service.
To install the random generator, use npm:
npm install --save restful-errors
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const RestfulErrors = require('restful-errors');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/bad-request', (req, res, next) => {
next(new RestfulErrors.BadRequestError());
})
app.get('/customized-messages', (req, res, next) => {
next(new RestfulErrors.BadRequestError([
{
location: 'query',
param: 'uid',
value: undefined,
msg: 'The uid field is required.'
},
{
location: 'body',
param: 'name',
value: 'A',
msg: 'The name field must be between 2 and 255 characters in length.'
}
]));
})
app.use(RestfulErrors.handler());
const server = http.createServer(app);
server.listen(3000);
module.exports = server;
npm install
npm run test
node-resful-errors is licensed under the MIT license.