express-json-router
v0.6.8
Published
Express router wrapper to send json response
Downloads
2,837
Maintainers
Readme
express-json-router
Express router wrapper to send json response
Installation
$ npm install express-json-router
$ yarn add express-json-router
Usage
const express = require('express');
const JsonRouter = require('express-json-router');
const router = new JsonRouter();
const clientErrors = JsonRouter.clientErrors;
const app = express();
// Optional error message provider
// Default to `error.message || error._message || error`
JsonRouter.errorMessageProvider = function (error) {
// Place an error parser here and return the new message.
return 'It is a processed error message';
};
router.all('/all-route', () => 'all-route');
router.get('/get-route', () => 'get-route');
router.post('/post-route', () => 'post-route');
router.put('/put-route', () => 'put-route');
router.delete('/delete-route', () => 'delete-route');
router
.route('/route-route')
.all((req, res, next) => next())
.get(() => 'route-get-route')
.post(() => 'route-post-route')
.put(() => 'route-put-route')
.delete(() => 'route-delete-route');
router.get(
'/next',
(req, res, next) => next(),
() => 'next-test'
);
router.get('/unauthorized-error', () => {
throw new clientErrors.UnauthorizedError();
});
app.use('/', router.original).listen();
Client Errors
- See https://www.npmjs.com/package/client-errors#client-errors-1 for more details.